no matching constructor for initialization of a Mock Factory - c++

I am trying for a while now to Unit-test my Factory with gmock/gtest, but I can't get my test to actually use the mock-object I want to test and at this point I feel like I'm doing something fundamentally wrong.
I have the following architecture (headers excluded)
with a Factory and ObjectFactory:
class IObject
{
public:
virtual ~IObject() {};
virtual void objectFunction(int someValue) = 0;
};
using ObjectPtr = std::unique_ptr<IObject>;
class IObjectFactory
{
public:
virtual ~IObjectFactory() {};
virtual std::unique_ptr<IObject> create() = 0;
};
using ObjectFactoryPtr = std::unique_ptr<IObjectFactory>;
The ObjectFactory Class returns an instance of the Object Class like this:
ObjectFactory.h
class ObjectFactory : public IObjectFactory
{
public:
ObjectFactory() {};
~ObjectFactory() override {};
std::unique_ptr<IObject> create() override
{
return std::make_unique<Object>();
}
};
I also have the Collection Class
ICollection.h
class ICollection
{
public:
virtual ~ICollection() {};
virtual void someFunction(int value) = 0;
};
Collection.h
class Collection : public ICollection
{
public:
Collection(IParameter *parameter, double& slider, FilterFactoryPtr&& filterFactory);
~Collection() override;
private:
ObjectFactoryPtr objectFactory_ {};
ObjectPtr object_ {};
The Collection Class gets the ObjectFactory injected in it's constructor and creates an instance of Object with it in the constructor like this:
Collection.cpp
Collection::Collection(IParameter *parameter, double aValue, ObjectFactoryPtr&& objectFactory)
: objectFactory (std::move(objectFactory))
{
object_ = objectFactory->create();
}
finally, in a function call of the Collection Class, objectFunction from the Object Class is called.
In order to test the behavior of Object, ObjectFactory and Collection I wrote some mocks like this:
ObjectMock.h
class ObjectMock : public IMock
{
public:
virtual ~ObjectMock() {}
MOCK_METHOD1(objectFunction, void(int someValue));
};
ObjectFactoryMock.h
class ObjectFactoryMock : public IObjectFactory
{
public:
virtual ~ObjectFactoryMock() {}
virtual std::unique_ptr<IObject> create()
{
return std::unique_ptr<dearvrDir::IObject>(createProxy());
}
MOCK_METHOD0(createProxy, IObject* ());
}
ParameterMock.h
class ParameterMock : public IParameterMock
{
public:
virtual ~ParameterMock() {}
MOCK_CONST_METHOD0(getValue, double());
}
finally, I want run the following test to validate the create() call of the objectFactory object:
class UnitTest_CollectionTestCase : public ::testing::Test
{
protected:
std::unique_ptr<Collection> collection_;
ParameterMock parameterMock_;
};
TEST_F(UnitTest_CollectionTestCase, calls_create_on_factory)
{
double value = 123;
collection_ = std::make_unique<Collection>(&parameterMock_, value, std::make_unique<ObjectFactoryMock>());
auto&& objectFactoryMock = std::make_unique<NiceMock<ObjectFactoryMock>>();
ON_CALL(*objectFactoryMock, create())
.WillByDefault(Return(std::make_unique<Object>));
}
However, instead of test results, I get the following error, hinting towards my Return expectation:
error: no matching function for call to 'ImplicitCast_'
value_(ImplicitCast_(value_before_cast_)) {}
^~~~~~~~~~~~~~~~~~~~~
note: in instantiation of member function 'testing::internal::ReturnAction > (*)()>::Impl, std::__1::default_delete > (*)(), IObject *()>::Impl' requested here
return Action(new Impl(value_));
^
note: in instantiation of function template specialization 'testing::internal::ReturnAction > (*)()>::operator Action' requested here
.WillByDefault(Return(std::make_unique));
I'm kind of hopeless at this point and would be happy to hear any suggestions on the topic.
thanks in advance,
Simon

Turns out I just had to add "ByMove" to my ON_CALL statement, to instruct my mock object not to invoke the copy constructor, which is deleted due to the unique_ptr.
Thus, the statement
ON_CALL(*objectFactoryMock, create())
.WillByDefault(Return(std::make_unique<Object>()));
has to be
ON_CALL(*objectFactoryMock, create())
.WillByDefault(Return(ByMove((std::make_unique<Object>())));

Related

ClassFactory automatic added to ClassTypeFactory

I got three classes. One ClassFactoryBase which looks like this:
class ClassFactoryBase {
public:
virtual ~ClassFactoryBase () = default;
virtual AbstractBase* Create() = 0;
};
I inherit from that class the actual Factory as a template
template <typename Type>
class ClassFactory final : public ClassFactoryBase {
ClassFactory (/*ClassFactoryBase& Factory*/); //Idea to automatically add them
~ClassFactory () = default;
AbstractBase* Create() override; //returns new Type
};
And ofc I implemented the Factory itself:
class ClassTypeFactory {
public:
template<typename Type>
void AddFactory(ClassFactoryBase& ClassFactory); // inserts ClassFactory into the map m_Factories
AbstractBase* Create(const std::string& ClassType);
private:
std::map<std::string, ClassFactoryBase&> m_Factories;
};
If I implement it like this I have to add to this Inside of my main.cpp
ClassTypeFactory class_type_factory;
ClassFactory<TopClass> top_state_factory(&class_type_factory);
class_type_factory.AddFactory<TopClass>(top_class_factory); // leave this out
This works just fine but I wonder if I could implement it in a way that I can leave out the last line. I think it should work because they both use the same template parameter.
I tried to give my ClassFactory a reference to a Factory and than call the AddFactory Method in there but I cant make it work. I always get inaccessible errors.
You just have to do it in correct order. Following modified version of your example (using smart pointer):
// Your objects to create:
struct AbstractBase
{
virtual ~AbstractBase () = default;
// ...
};
struct Derived : AbstractBase
{
// ...
};
// Your abstract factory
class ClassFactoryBase {
public:
virtual ~ClassFactoryBase () = default;
virtual std::unique_ptr<AbstractBase> Create() = 0;
};
// Your factory collection
class ClassTypeFactory {
public:
void AddFactory(const std::string& name, ClassFactoryBase& factory) { m_Factories.emplace(name, std::ref(factory)); }
std::unique_ptr<AbstractBase> Create(const std::string& name)
{
return m_Factories.at(name).get().Create();
}
private:
std::map<std::string, std::reference_wrapper<ClassFactoryBase>> m_Factories;
};
// Your real factory
template <typename Type>
class ClassFactory final : public ClassFactoryBase {
public:
ClassFactory(const std::string& name, ClassTypeFactory& factory) { factory.AddFactory(name, *this); }
std::unique_ptr<AbstractBase> Create() override { return std::make_unique<Type>(); }
};
And then usage:
ClassTypeFactory class_type_factory;
ClassFactory<Derived> top_state_factory("TopClass", class_type_factory);
auto ptr = class_type_factory.Create("TopClass");
Demo

EXPECT_CALL on template mock

I have mock defined as follows:
template<typename T>
class ParseTreeMock : public ParseTreeInterface<T> {
public:
MOCK_METHOD1(fillConfigTree, void(std::string const&));
MOCK_METHOD1_T(getProperty, T(std::string const&));
ParseTreeMock(): parseTree(std::make_unique<pt::ptree>()) {
}
static std::unique_ptr<ParseTreeInterface<T>> getDefaultTree() {
return std::make_unique<ParseTreeMock<T>>();
}
private:
std::unique_ptr<pt::ptree> parseTree;
};
which is created later in test case:
class ConfigTest : public ::testing::Test {
protected:
std::unique_ptr<ParseTreeInterface<std::string>> treeMock;
virtual void SetUp() {
treeMock = ParseTreeMock<std::string>::getDefaultTree();
}
};
And I want to set return specific value on getProperty method:
EXPECT_CALL(*treeMock, getProperty("miniReaderConfig.cacheConfig.cacheOnOff")).willOnce(Return(false));
I get error:
In file included from ./lib/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:43:0,
from ./lib/googletest/googlemock/include/gmock/gmock.h:61,
from ./test/UT/Mocks/ParseTreeMock.hpp:2,
from test/UT/Configuration/ConfigTest.cpp:1:
test/UT/Configuration/ConfigTest.cpp: In member function ‘virtual void ConfigTest_CreateConfigurationWithoutErrors_Test::TestBody()’:
./lib/googletest/googlemock/include/gmock/gmock-spec-builders.h:1844:12: error: ‘class miniReader::Configuration::ParseTreeInterface<std::__cxx11::basic_string<char> >’ has no member named ‘gmock_getProperty’; did you mean ‘getProperty’?
((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
Any solution with explanation of error appreciated.
The treeMock variable needs to be of type std::unique_ptr<ParseTreeMock<std::string>>, and then static method needs to look like this
static std::unique_ptr<ParseTreeMock<T>> getDefaultTree()
{
return std::make_unique<ParseTreeMock<T>>();
}
Typically you instantiate a class that implements an interface in your test and then pass the instance to the class that you are testing, and with EXPECT_CALLs you make sure that the class you are testing calls the callbacks on your mock object.
Not related to the error you got, but WillOnce needs to be spelled with the first letter capitalized. Also, since you set the template variable to std::string, the EXPECT_CALL can't expect that a boolean is returned.
This compiles for me:
namespace pt { struct ptree {};}
template<typename T>
class ParseTreeInterface
{
public:
virtual void fillConfigTree(std::string const&) = 0;
virtual T getProperty(std::string const&) = 0;
};
template<typename T>
class ParseTreeMock : public ParseTreeInterface<T> {
public:
MOCK_METHOD1(fillConfigTree, void(std::string const&));
MOCK_METHOD1_T(getProperty, T(std::string const&));
ParseTreeMock(): parseTree(std::make_unique<pt::ptree>()) {
}
static std::unique_ptr<ParseTreeMock<T>> getDefaultTree()
{
return std::make_unique<ParseTreeMock<T>>();
}
private:
std::unique_ptr<pt::ptree> parseTree;
};
class ConfigTest : public ::testing::Test {
protected:
std::unique_ptr<ParseTreeMock<std::string>> treeMock;
virtual void SetUp() {
treeMock = ParseTreeMock<std::string>::getDefaultTree();
}
};
TEST_F(ConfigTest, test)
{
EXPECT_CALL(*treeMock, getProperty("miniReaderConfig.cacheConfig.cacheOnOff")).WillOnce(::testing::Return(""));
}

Calling the right free function from a base pointer/reference

Let a class hierarchy :
class Base { virtual ~Base() throw(); };
class DerivedA : public Base { };
class DerivedB : public Base { };
I would like to have some code specific to each of these derived classes. However that code also being specific to the application that makes use of this class hierarchy, I do not want to embbed this derived-class-specific code into these derived classes. To avoid doing so, I thought about writing free functions :
void DerivedASpecificWork( DerivedA da );
void DerivedBSpecificWork( DerivedB db );
However, when given an instance of a derived class through a reference/pointer to a Base, I do not have access to the actual type of the instance, and thus cannot call the proper Derived*SpecificWork() function.
I would like to know if there is nome kind of design pattern that would allow me to call a derived-class-specific function without knowing the actual type of the instance, i.e having the same mechanism as virtual functions provide, but without having these virtual functions that would require me to embbed application-specific code into that class hierarchy.
Actually, why I want to do that is to provide informations about an exception that occured within a natively implemented function called by a Lua script. Each exception carrying its own set of information, the way I want to represent the error within the script depends on the type of the exception. I could create a pure virtual method in the base class that would be implemented by derived classes, but this would require me to embbed Lua-related code into my exception hierarchy, which I do not want to do since the Lua is specific to one of the application using that exception hierarchy.
Also I cannot use C++11.
Thank you.
May be Brigde pattern can help you.
This pattern can be used when you want to avoid a permanent binding between an abstraction and it's implementation.
(I don't see your comment about your restriction in using c++11, but you can remove std::unique_ptr, std::move and override keyword)
class AppSpecificImp
{
public:
virtual void DoWork() = 0;
};
class Base
{
public:
virtual ~Base() throw();
virtual DoWork() = 0;
};
class DerivedA : public Base
{
public:
DerivedA(std::unique_ptr<AppSpecificImp> appImp)
: imp(std::move(appImp))
{
}
void DoWork() override
{
// DerivedA specific code
imp->DoWork();
}
private:
std::unique_ptr<AppSpecificImp> imp;
};
class DerivedB : public Base
{
public:
DerivedB(std::unique_ptr<AppSpecificImp> appImp)
: imp(std::move(appImp))
{
}
void DoWork() override
{
// DerivedB specific code
imp->DoWork();
}
private:
std::unique_ptr<AppSpecificImp> imp;
};
Edit to show Visitor pattern usage:
With visitor pattern you can do what you want but with more Effort.
class Visitor
{
public:
virtual void VisitDerivedA(DerivedA* object) = 0;
virtual void VisitDerivedB(DerivedB* object) = 0;
};
class Base
{
public:
virtual void Visit(Visitor* visitor) = 0;
};
class DerivedA : public Base
{
public:
virtual void Visit(Visitor* visitor)
{
visitor->VisitDerivedA(this);
}
};
class DerivedB : public Base
{
public:
virtual void Visit(Visitor* visitor)
{
visitor->VisitDerivedB(this);
}
};
class AppSpecificVisitor : public Visitor
{
public:
void VisitDerivedA(DerivedA* object)
{
// Do any work related to DerivedA class
}
void VisitDerivedB(DerivedB* object)
{
// Do any work related to DerivedB class
}
}
int main()
{
AppSpecificVisitor myVisitor;
Base* myBase = // any class in your hierarchy
myBase->Visit(&myVisitor);
}
As I said in comments with Visitor pattern you can add new functionally without changing the main hierarchy(Base->Derived types). You just define a new visitor implementation and write your logic for every class in main hierarchy. In your example you can pack app specific logic in an object and reference that in your derived objects that is an easier approach.
Why not using a new set of hierarchy for application specific implementation ?
class AppBase
{
public:
virtual ~AppBase() throw();
virtual void work_with_app() = 0;
};
class Base
{
public:
Base(AppBase& app) : m_app(app) {}
virtual ~Base() throw();
protected:
AppBase& m_app;
};
class DerivedA : public Base { DerivedA(AppBase& app) : Base(app) {} };
class DerivedB : public Base { DerivedA(AppBase& app) : Base(app) {} };
// Application specific implementation :
class AppLuaSpecific : public AppBase
{
public:
void work_with_app() { /* Lua app specific */ }
};
This way, your 1st hierarchy : Base, DerivedA, DerivedB can live without knowing anything about the app specific code implemented in AppLuaSpecific.
You can implement your own app-specific dispatch as follows (check it live on Coliru):
#include <iostream>
#include <typeinfo>
struct Base { virtual ~Base() {} };
struct DerivedA : public Base { };
struct DerivedB : public Base { };
namespace AppSpecific
{
template<class F>
void dispatch(const Base& b)
{
const std::type_info& t = typeid(b);
if ( t == typeid(DerivedA) )
F::doit(static_cast<const DerivedA&>(b));
else if ( t == typeid(DerivedB) )
F::doit(static_cast<const DerivedB&>(b));
}
struct Foo
{
static void doit(const DerivedA& da) { std::cout << "Foo(DerivedA)\n"; }
static void doit(const DerivedB& db) { std::cout << "Foo(DerivedB)\n"; }
};
struct Bar
{
static void doit(const DerivedA& da) { std::cout << "Bar(DerivedA)\n"; }
static void doit(const DerivedB& db) { std::cout << "Bar(DerivedB)\n"; }
};
} // namespace AppSpecific
int main()
{
DerivedA da;
DerivedB db;
Base& b1 = da;
Base& b2 = db;
AppSpecific::dispatch<AppSpecific::Foo>(b1);
AppSpecific::dispatch<AppSpecific::Foo>(b2);
AppSpecific::dispatch<AppSpecific::Bar>(b1);
AppSpecific::dispatch<AppSpecific::Bar>(b2);
}

wrapper to template class inherited by another class

template <class CollectionItem>
class Collection
{
void A();
// Many other utility functions
}
class ICollection
{
virtual void B() = 0;
}
class Base : public Collection<BaseItem>, public IBase
{
virtual void B();
}
Is there any way of offering Collection functions via ICollection interface without wrapping all the functions in Base class? ICollection : public Collection<CollectionItem> is not an option.
Bounty Update:
OK, so the original idea was to have Interface to all Collection classes. Before we continue, every CollectionItem also has Interface, let's call it ICollectionItem and ICollection only knows about ICollectionItem.
So what I did was create another template class as Interface to Collection template class - ICollection (pure virtual) accepting ICollectionItem(s). Collection class inherits this interface.
Every Collection class (inheriting Collection<CollectionItem> class) would also inherit it's Interface Collection class. That Interface then virtual inherits ICollection<ICollectionItem>. I'll just post the code :)
Here is the code:
template <class ICollectionItem>
class ICollection
{
public:
virtual const ICollectionItem* At(const int idx) = 0;
};
template <class CollectionItem, class ICollectionItem>
class Collection
: public ICollection,
public virtual ICollection<ICollectionItem> // Weak point
{
private:
List<CollectionItem*> fContainer;
public:
Collection(void) {}
virtual ~Collection() {}
virtual const ICollectionItem* At(const int idx); // Casting GetAt result
virtual const TCollectionItem& GetAt(const int idx) const
virtual ListIterator<TCollectionItem> >* GetIterator(void) const;
virtual ListIterator<ICollectionItem> >* Iterator(void) const; // Weak point
}
Example usage:
class IBaseItem
{
public:
virtual int Number() = 0;
{
class BaseItem
: public IBaseItem
{
public:
virtual int Number();
void SetNumber(int value);
}
class IBase
: public virtual ICollection<IBaseItem>
{
public:
virtual IBaseItem* ItemByName(String name) = 0;
virtual ~IBase() {}
}
class Base
: public Collection<BaseItem, IBaseItem>,
public IBase
{
public:
BaseItem* GetItemByName(String name);
virtual IBaseItem* ItemByName(String name);
}
Weak points:
First is at using virtual inheritance ... lots written about it, not much to talk about, or is it?
Unable to access Iterator using ICollection interface. See ListIterator function, only first one can be implemented, the second one would require some kind of new List of IBaseItem. I decided to live with that and just use for loop.
Even tho I somehow managed to get what I wanted (With wrapping and casting), I would still like to hear an second opinion. I don't like using virtual inheritance, specially in such delicate situations - using Collections for application Base creation.
I can not see any other solution than calling some Collection method in Base implementation of IBase virtual methods.
class Base : public Collection<BaseItem>, public IBase
{
virtual void B()
{
A();
}
}
You say, and I quote:
I want to call Collection functions using IBase pointer
I really don't see what is to be done here besides dynamic_cast. It does exactly what you want it to do.
void fun(IBase * base) {
auto * coll = dynamic_cast<Collection<BaseItem>*>(base);
if (coll) {
coll->A();
}
}
Your Collection class must have a virtual destructor.
You can, of course, offer a templated version, if you'd need different baseitems in different, scenarios for some reasons. This has bad code smell and I think your architecture is bad at this point, but oh well.
template <typename T> void fun(IBase * base) {
auto * coll = dynamic_cast<Collection<T>*>(base);
if (coll) {
coll->A();
}
}
void test(IBase * p) {
fun<BaseItem5>(p);
}
If you have some other specific scenario in mind, please edit your question to say what you mean.
Hmm...So you wanna to reuse the Collection class's utility functions, and you want to design a class which will implement an interface defined by IBase. As you mentioned above,"wrapping all the functions in Base class" is a way to offer Collection functions.
(1) Via inheritance,derived class has a good knowledge of Collection
class Derived:public Collection<DerivedType>,public IBase{};
or
template <typename T>
class Derived:public Collection<T>,public IBase{};
(2) Via inheritance,derived class knows little about Collection,but through IBase
class IBase : public Collection<BaseItem>{};
class Derived:public IBase{};
By (1),If you want to call Collection functions using IBase pointer,you have to wrap the functions.
By (2), any Derived instance is " a kind of " IBase which is "a kind of " Collection. So you can use IBase pointer to call Collection functions.
So,the key point is that the objects pointed by the IBase pointer should have the method you want to call.Wrap it or inherit it. I can not see any other solution than these two ways.
Edit: the idea is refined based on your example:
Here is an idea:
//generic interface can be kept as it is
template <class ICollectionItem>
class ICollection
{
public:
virtual const ICollectionItem* At(const int idx) = 0;
};
class Empty
{
};
template <class CollectionItem , class BaseClass = Empty>
class GenericCollection
: public BaseClass
{
public:
const CollectionItem* At(const int idx);
// At and ItemByName are standard functions for a collection
CollectionItem* ItemByName(String name);
//note that here nothing has to be declared as virtual
};
//example usage:
class IBase
: public virtual ICollection<IBaseItem>
{
public:
virtual IBaseItem* ItemByName(String name) = 0;
virtual ~IBase() {}
};
class Base
: public GenericCollection<BaseItem, IBase >
{
public:
//nothing to be implemented here, all functions are implemented in GenericCollection and defined as virtual in IBase
//The definition of the functions has to be the same:
};
In collection you can implement whatever and in the interface you can define what ever you want to be virtual from your collection. The only thing is that you need to have some standard in naming convention for functions.
Hope this helps,
Raxvan.
From your comments in another answer, it seems you want a collection of interfaces, and an implementation of this interface. The simplest I can advise you is the following:
template<typename T>
class ICollection
{
public:
virtual iterator<T>* begin() const = 0;
};
template<typename T, typename TBase>
class Collection : public ICollection<TBase>
{
public:
iterator_impl<T>* begin() const { return whatever; }
};
Example:
class IItem {};
class Item : public IItem {};
class Base : public Collection<Item, IItem> {};
old answer:
Is there any way of offering Collection functions via IBase interface without wrapping all the functions in Base class ?
If I understood your problem, you want to use it like this:
void myfunc()
{
// ...
IBase* obj = ...;
obj->A();
obj->B();
}
I think here is a misunderstanding here: if you want A() to be callable from an IBase, then you have to add it to Ibase declaration.
If you want to use the Collection functions on an object, then you should cast this object to a Collection, via dynamic_cast for example.
Furthermore, if you have such a funcion:
void fun(IBase* base) { /* ... */ }
you cannot cast to a Collection*, since there are no relationship between these two classes, unless you have another way to be sure base is a Collection:
void fun(IBase* base)
{
if(base && base->isABaseItemCollection())
{
// Valid, since the real type was checked before
Collection* collection = (Collection*)base;
// ...
}
}
On a side note: you can generate bases almost automatically:
template
class Base : public Collection, public U {};
typedef Base BaseCollection;
According to comment/chat:
You have something like:
class IAnimal { /*...*/ };
class Cat : public IAnimal { /*...*/ };
class Dog : public IAnimal { /*...*/ };
class Cats
{
std::vector<Cat*> cats;
public:
Cat* at(size_t index) { return cats[index]; }
/*...*/
};
class Dogs
{
std::vector<Dog*> dogs;
public:
Dog* at(size_t index) { return dogs[index]; }
/*...*/
};
And you want to factorize some code using something like
class IAnimals
{
public:
std::vector<IAnimals*> animals; // or getter/setter which works with IAnimals.
/* some common factorized code */
};
// And so
class Cats : public IAnimals { /**/ };
class Dogs : public IAnimals { /**/ };
I propose, instead of creating class IAnimals, to use template functions as:
template <typename TAnimals>
void foo(TAnimals& animals)
{
Ianimals* animal = animals.at(42);
// ...
animal->eat(food);
// ...
}
You have to give compatible "interface" (names) to the type used in template.
Maybe you could have an operator() in IBase that would be delegated to Base?
class CollectionBase {};
template <class Item> class Collection: public CollectionBase {};
class IBase
{
public:
virtual CollectionBase* operator()() = 0;
};
class Base : public Collection<BaseItem>, public IBase
{
public:
virtual Collection<BaseItem>* operator()() { return this; }
};

C++ Inheritance with pure virtual functions

I'm trying to create a class that serves as a base object, which will then be sub-classed (=implemented) to serve various purposes.
I want to define one or more pure virtual functions, so that however subclasses the base class, is required and does not forget to implement them.
There is one caveat, the pure virtual function's signature includes the type of the base object. Once sub-classed, the function definition doesn't match the base classes definition anymore of course. E.g.:
class BaseItem
{
public:
virtual std::string getDifferences(const BaseItem& item) = 0;
}
So, in the derived class I'd like to do:
class DerivedClass : public BaseItem
{
public:
virtual std::string getDifferences(const DerivedClass& item) = 0;
private:
std::string derivedItemCustomObject;
}
which of course the compiler won't accept. I could make it a BaseItem of course, but then I can't utilize any objects in the derived class.
Do I have to use casting to accomplish this?
Please let me know if my intent/question is not clear.
There is NO need to change the function signature. Look at following:
class BaseItem
{public:
virtual std::string getDifferences(const BaseItem& item) = 0;
};
class DerivedClass : public BaseItem
{public:
virtual std::string getDifferences(const BaseItem& item) // keep it as it's
{
const DerivedClass& derivedItem = static_cast<const DerivedClass&>(item);
}
};
Can use static_cast<> without any fear because, DerivedClass::getDifferences() is called only for DerivedClass object. To illustrate,
BaseItem *p = new DerivedClass;
DerivedClass obj;
p->getDifferences(obj); // this always invoke DerivedClass::getDifferences
If you worry that sometime you might end up passing any other derived class object as an argument to the method, then use dynamic_cast<> instead and throw exception if that casting fails.
It's unclear what you're trying to achieve. Suppose that the compiler allowed you to do this (or you do this by the means of a cast), then it would open the following hole in the type system:
class BaseItem
{
public:
virtual std::string getDifferences(const BaseItem& item) = 0;
};
class DerivedClass : public BaseItem
{
public:
virtual std::string getDifferences(const DerivedClass& item)
{
item.f();
// ...
}
void f() const {}
};
class DerivedClass2 : public BaseItem
{
public:
virtual std::string getDifferences(const DerivedClass2& item) { ... }
};
void g()
{
BaseItem* x = new DerivedClass;
// oops, calls DerivedClass::f on an instance of DerivedClass2
x->getDifferences(DerivedClass2());
}
Your design is probably wrong.
I assume that the compiler accept but DerivedClass::getDifferences doesn't override BaseItem::getDifferences. Here is a way to achieve what you apparently want
template <typename T>
class DerivedHelper: public BaseItem {
public:
virtual std::string getDifferences(const BaseItem& item) {
getDifferences(dynamic_cast<const T&>(item));
}
virtual std::string getDifferences(const T& item) = 0;
};
class DerivedClass : public DerivedHelper<DerivedClass>
{
public:
// not more needed but providing it will hide getDifferences(const BaseItem& item)
// helping to statically catch some cases where a bad argument type is used.
virtual std::string getDifferences(const DerivedClass& item) = 0;
private:
std::string derivedItemCustomObject;
};
but be aware that there is a runtime check which will throw exceptions if the argument isn't of the correct class.
One way to accomplish this is to use a template and have the parameter be the type of the derived type
template <typename T>
class BaseItem {
public:
virtual std::string getDifferences(const T& item) = 0;
};
class DerivedClass : public BaseItem<DerivedClass> {
public:
virtual std::string getDifferences(const DerivedClass& item) {
// Implement it here
}
};
You should use cast from BaseItem to DerivedClass + runtime check if given BaseItem is a DerivedClass instance.