For example, I have the base class with pure virtual functions:
class IBase
{
virtual void Function(const IBase& ref) = 0;
};
If I inheriet the class, do I have to overload ‘Function’ which takes the derived class as a parameter?
class Derived
{
// this will be implemented
virtual void Function(const IBase& ref) {}
// does this have to be implemented
virtual void Function(const Derived& ref) {}
};
Or can the compiler differentiate between the calls and I can skip writing the overload function?
Derived d();
...
IBase* dptr = &d; // ignoring cast for example
// would never really call 'Function' on itself, this is for example purposes
dptr->Function(d);
Notes: IBase::Function must take reference type, not pointer type.
I understand the rules of inheriting pure-virtual functions, just not this special case where the pure virtual function takes the base type as a parameter.
What I need to know is do I have to implement an overload in each inherited type that takes the inherited type as a parameter, or will the compiler understand that if I pass a Derived reference, to call on the virtual implementation?
Yes, if you have a
Function(const IBase&)in the base class and override it in the derived class, you can pass references to the derived class toFunctionand theFunction(const IBase&)will be called.