I have following C++ code
AbstractClass_01.h
class AbstractClass_01 {
public:
virtual void method_01() = 0;
};
AbstractClass_02.h
class AbstractClass_02 {
public:
virtual void method_02() = 0;
};
ClassA.h
#include "AbstractClass_02.h"
class ClassA : public AbstractClass_02{
public:
void method_02();
void method_03();
};
ClassA.cpp
#include "ClassA.h"
void ClassA::method_02() {}
void ClassA::method_03() {}
ClassB.h
#include "AbstractClass_01.h"
#include "AbstractClass_02.h"
class ClassB : public AbstractClass_01 {
public:
ClassB(AbstractClass_02& _obj);
void method_01();
private:
AbstractClass_02 &obj;
};
ClassB.cpp
#include "ClassB.h"
#include "ClassA.h"
ClassB::ClassB(AbstractClass_02& _obj) : obj(_obj) {}
void ClassB::method_01() {
static_cast<ClassA&>(obj).method_03();
}
main.cpp
#include "ClassA.h"
#include "ClassB.h"
int main(int argc, char** argv) {
ClassA a;
ClassB b(a);
b.method_01();
return 0;
}
My question is whether the casting which I have done in the ClassB::method_01 is common C++ construct whether it is a sign of wrong design?
EDIT:
The reason why the ClassB constructor accepts references to the AbstractClass_02 instead of the ClassA is that I need to pass references to various objects which all have common interface AbstractClass_02. The problem is that this interface doesn't contain the method_03. One possible solution could be to append that method into the interface AbstractClass_02 but that method isn't cohesive with the method_02.
This is known as downcasting and is typically a sign of bad design. Based on the limited knowledge we have, it seems obvious that you should take ClassA in constructor instead of AbstractClass_01
#include "AbstractClass_01.h"
#include "ClassA.h"
class ClassB : public AbstractClass_01 {
public:
ClassB(ClassA & _obj);
void method_01();
private:
ClassA &obj;
};
But we don't know your real problem and why did you decide to go with casting or accepting interface in the first place.
Related
I have been attempting to use GoogleMock to override a few specific methods in a underlying class, however I seem to be getting the base constructor, rather than the mocked object. Is there something obvious I am missing here?
I have been following the following example:
http://blog.divebomb.org/2011/07/my-first-c-cmake-googletest-and-googlemock/
However, in my test, I am still getting my 'printf' called. Any thoughts?
Here are the classes/header files:
A.h:
#pragma once
class A
{
public:
virtual void methodToOverride();
void someConcreteMethod();
int mMemberVariable;
};
A.cpp:
#include "A.h"
void A::methodToOverride()
{
std::printf("Hello World");
}
void A::someConcreteMethod()
{
}
B.h:
#include "A.h"
class B
{
public:
B(A &injectedClass);
~B();
void MethodToTest();
private:
A mA;
};
B.cpp:
#include "B.h"
B::B(A & injectedClass):mA(injectedClass)
{
mA.someConcreteMethod();
}
B::~B(){}
void B::MethodToTest()
{
mA.methodToOverride();
}
MockA.h:
#include "A.h"
#include "gmock\gmock.h"
class MockA : public A
{
public:
MOCK_METHOD0(methodToOverride, void());
};
BTest.cpp:
#include "gtest/gtest.h"
#include "MockA.h"
#include "B.h"
using ::testing::AtLeast;
using ::testing::_;
TEST(BTest, mockObject)
{
// Arrange
MockA injectedMock;
EXPECT_CALL(injectedMock, methodToOverride())
.Times(AtLeast(1));
B classUnderTest(injectedMock);
// Act
classUnderTest.MethodToTest();
}
One major problem is that B::mA is an instance of the A class. It doesn't know anything about child-classes and objects.
The member B::mA either needs to be a reference or a pointer for polymorphism to work.
I have a function1 inside which function 2 is called. I have to mock only function2, whwenever i call function1 it should call real implementation of function1 and mock implementation of function2. Kindly help me on this
Display.cpp
#include "Display.h"
int DisIp::getip()
{
return 5;
}
int DisIp::display()
{
Addition obj;
int ip=obj.getip();
return ip;
}
Display.h
class DisIP
{
public:
int display();
int getip();
};
GMOCK file
#include <limits.h>
#include "gmock.h"
#include "gtest.h"
#include "Display.h"
#include <string>
using namespace std;
using ::testing::AtLeast;
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Gt;
using ::testing::Return;
using testing::ReturnPointee;
using ::testing::Invoke;
class MyInterface{
public:
virtual int display() = 0;
virtual int getip()=0;
};
class MockInter : public MyInterface
{
public:
MockInter()
{
ON_CALL(*this, getip()).WillByDefault(Invoke(&this, &MockInter::getip));
ON_CALL(*this, display()).WillByDefault(Invoke(&real, &Addition::display));
}
MOCK_METHOD0(display,int());
MOCK_METHOD0(getip,int());
DisIp real;
};
class DisplayTest : public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
// Code here will be called immediately after each test
// (right before the destructor).
}
};
TEST_F(DisplayTest,ip){
MockInter mock;
//EXPECT_EQ(1,mock.display());
EXPECT_EQ(1,mock.getip());
}
Your design suffers from breaking Single Responsibility Principle.
Displaying and getting IP are two different responsibilities. It is even shown in your implementation of DisIp::display() - you get IP from so-called Addition obj. When you fix this design error - your unit tests becomes much easier and straightforward. But it is important to say that UT are only the symptom here, the bad design is a disease.
So how it could look like:
class IIpProvider
{
public:
virtual ~IIpProvider() = default;
virtual int getIp() = 0;
};
class DispIp
{
public:
DispIp(IIpProvider& ipProvider) : ipProvider(ipProvider) {}
int display()
{
int ip=ipProvider.getIp();
//...
return ip;
}
private:
IIpProvider& ipProvider;
};
then your Mock:
class IpProviderMock : public IIpProvider
{
public:
MOCK_METHOD0(getIp, int());
};
And your tests:
class DispIpTest : public ::testing::Test
{
protected:
IpProviderMock ipProviderMock;
DispIp objectUnderTest{ipProviderMock}; // object-under-test must be connected to object doubles (like mocks)
};
TEST_F(DispIpTest, shallUseProvidedIpToDisplay)
{
using namespace testing;
auto SOME_IP = 7;
EXPECT_CALL(ipProviderMock, getIp()).WillRepeatedly(Return(SOME_IP));
//...
ASSERT_EQ(SOME_IP, objectUnderTest.display());
}
In your original tests - main problem was also that your mock object was not connected in any way to your object under test.
If you do not like (cannot) to change your design (what I really advice) you have to use technique called partial mocking
In your case - it would something like this:
class DisIP
{
public:
int display();
virtual int getip(); // function for partial mocking must be virtual
};
class DisIPGetIpMock : public DisIP
{
public:
MOCK_METHOD0(getIp, int());
};
class DispIpTest : public ::testing::Test
{
protected:
DisIPGetIpMock objectUnderTest;
};
TEST_F(DispIpTest, shallUseProvidedIpToDisplay)
{
EXPECT_CALL(objectUnderTest, getIp()).WillRepeatedly(Return(SOME_IP));
...
ASSERT_EQ(SOME_IP, objectUnderTest.display());
}
You can use the Cutie library to mock C function GoogleMock style, if that will assist you.
There's a full sample in the repo, but just a taste:
INSTALL_MOCK(fclose);
CUTIE_EXPECT_CALL(fclose, _).WillOnce(Return(i));
Please help me to understand strange behavior:
I use dynamic_cast from MyObject to MyLogicObject when a destructor ~MyLogicObject() in processing, but compiler throw an exception: non_rtti_object.
I'm sure that object MyObject is a polymorph type. Where am I wrong?
#ifndef MYOBJECT_H
#define MYOBJECT_H
#include <string>
class A
{
int a;
};
class B
{
int b;
};
class MyObject: public A,
public B// if comment this row, and don't use multi inheritable, everything will be fine
{
private: std::string name;
private: bool singleshot;
public: MyObject(void);
public: virtual ~MyObject(void);
protected: void Destroying(void);
public: std::string GetName(void);
public: virtual bool Rename(std::string _newName);
};
#endif
#include "MyObject.h"
#include "MyLogicObject.h"
MyObject::MyObject(void): singleshot(true)
{}
MyObject::~MyObject(void)
{
printf("\n~my object\n");
Destroying();
}
void MyObject::Destroying(void)
{
if(singleshot)
{
printf("\nexception!\n");
dynamic_cast<MyLogicObject*>(this);// exception: non_rtti_object
singleshot = false;
}
}
std::string MyObject::GetName(void)
{
return name;
}
bool MyObject::Rename(std::string _newName)
{
name = _newName;
return true;
}
#ifndef MYLOGICOBJECT_H
#define MYLOGICOBJECT_H
#include "MyObject.h"
class MyLogicObject: public virtual MyObject // if not use virtual inheritance (instead, use the standard inheritance), everything will be fine
{
public: MyLogicObject(void);
public: virtual ~MyLogicObject(void);
public: virtual void Update(float _delta = 0.0f);
// if reimplement virtual method of base class, everything will be fine
/*
public: virtual bool Rename(std::string _newName)
{
return MyObject::Rename(_newName);
}
*/
};
#endif
#include "MyLogicObject.h"
MyLogicObject::MyLogicObject(void)
{}
MyLogicObject::~MyLogicObject(void)
{
printf("\n~my logic object\n");
Destroying();
}
void MyLogicObject::Update(float _delta)
{}
#include <conio.h>
#include <stdio.h>
#include "MyLogicScene.h"
class C
{
int c;
};
class DerivedObject: public MyLogicObject,
public C// if comment this row, and don't use multi inheritable, everything will be fine
{
public: DerivedObject(void)
{}
public: virtual ~DerivedObject(void)
{
printf("~derived object: %s\n", GetName().c_str());
//Destroying(); // if call Destroying in this place, overything will be file
}
};
int main()
{
DerivedObject* object1 = new DerivedObject();
object1->Rename("object1");
printf("delete object1...\n");
delete object1;
getch();
return 0;
}
You are trying to dynamic cast an object of a base class (MyObject) type to the derived class (MyLogicObject). And this conversion is not allowed with dynamic_cast unless the base class is polymorphic and rtti is enabled. See this for reference.
So you basically need to enable rtti in your compiler options.
Once that is done make sure that object1 is a complete object of the derived class (MyLogicObject) for the cast to work without raising an exception.
It would work in the opposite scenario too. If you were trying for example to dynamic cast an object of a derived class (MyLogicObject) type to the base class (MyObject).
I have a very simple base class/ derived class scenario:
#include <tchar.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
class BaseChannel{
private:
public:
virtual ~BaseChannel(){};
virtual void SayBoo( bool SelectAll)=0;
};
class gdbChannel: BaseChannel{
public:
void SayBoo(bool SelectAll) {if (SelectAll) cout<<"boo";}
};
class UsesChannel{
public:
BaseChannel * c;
};
int _tmain(int argc, _TCHAR* argv[])
{
gdbChannel gc;
UsesChannel uc;
// uc.c = &gc; //cannot convert gdbChannel* to BaseChannel*
uc.c = (BaseChannel*)&gc; // works
uc.c->SayBoo(true);
getch();
return 0;
}
My initial implementation, commented out above, will not compile. Can anyone explain why? I feel that since gdbChannel is explicitly an instance of BaseChannel the cast shouldn't be necessary.
That is because gdbChannel does not use public inheritance to derive from BaseChannel (the default for class types is private).
If public inheritance is what you wanted to express, just write:
class gdbChannel : public BaseChannel{
// ^^^^^^
public:
void SayBoo(bool SelectAll) {if (SelectAll) cout<<"boo";}
};
I have a problem creating some form of hierarchy with different object types. I have a class which has a member of another class, like this:
class A
{
public:
A(){}
~A(){}
void addB(B* dep){
child = dep;
dep->addOwner(this);
}
void updateChild(){
child->printOwner();
}
void print(){
printf("Printing...");
}
private:
B* child;
};
And this is class B:
class B
{
public:
void addOwner(A* owner){
ownerObject = owner;
}
//ISNT WORKING
void printOwner(){
ownerObject->print();
}
private:
A* ownerObject;
};
Calling a function of "B" out of class "A" works just fine but trying it vice versa gives a compiler error because A is not defined in B. It actually is by using an include and a forward declaration, but I guess its a cross reference problem which the compiler can not solve.
Is there any chance to solve this problem or should I rethink my design?
You say that you already solved your circular dependency problem by using a forward declaration of A instead of including the header where A is defined, so you already know how to avoid circular includes. However, you should be aware of what is possible and what is not with incomplete types (i.e. types that have been forward declared).
In your case, you try to call the member function print on an object that has an incomplete type; the compiler knows nothing about this type excepts that it will be defined at some point, so it does not allow you to do this. The solution is to remove the implementation of the printOwner member function from the B header and put it into an implementation file:
//B.hpp
class A; // forward declaration
class B
{
public:
void addOwner(A* owner);
void printOwner() const; // I think this member function could be const
private:
A* ownerObject;
};
//B.cpp
#include "B.hpp"
#include "A.hpp" // here we "import" the definition of A
void B::addOwner(A * owner)
{
ownerObject = owner;
}
void B::printOwner() const
{
ownerObject->print(); //A is complete now, so we can use its member functions
}
You could possibly do the same thing in the A header.
You can use forward declaration, and define the member functions outside of the class, i.e.
// A.h
class B;
class A { public:
void addB(B* dep); // don't define addB here.
...
};
// B.h
class A;
class B { public:
void addOwner(A* owner); // don't define addOwner here.
...
};
// A.cpp
#include "A.h"
#include "B.h"
void A::addB(B* dep) {
...
}
// B.cpp
// similar.
You probably should rethink your design, since a crcular parent-child relationship is usually a code smell.
But, you can make the compiler happy :
#include <cstdlib>
#include <cstdio>
class A
{
public:
A(){}
~A(){}
void addB(class B* dep);
void updateChild();
void print(){
printf("Printing...");
}
private:
class B* child;
};
class B
{
public:
void addOwner(A* owner){
ownerObject = owner;
}
//ISNT WORKING
void printOwner(){
ownerObject->print();
}
private:
A* ownerObject;
};
void A::addB(class B* dep){
child = dep;
dep->addOwner(this);
}
void A::updateChild(){
child->printOwner();
}
int main()
{
return 0;
}
You should move B::printOwner implementation to .cpp file.