Overriding virtual function in C ++
I have a base class with a virtual function - int Start(bool)
In a derivative, there is a function with the same name but with a different signature -
int Start(bool, MyType *)
But not virtually
In a derivative, Start()
I want to call the base classStart()
int Derived::Start(bool b, MyType *mType)
{
m_mType = mType;
return Start(b);
}
But it gives a compilation error.
"Start' : function does not take 1 arguments"
However Base::Start(b)
works
In C #, the above code works, so no reference to Base is required to resolve the call.
Externally if the call is done like this
Derived *d = new Derived();
bool b;
d->Start(b);
Failed: message
Start : function does not take 1 arguments
But in C #, the same scenaio works.
As I understand it, the virtual mechanism cannot be used to resolve the call because the two functions have different signatures.
But the challenges will not be resolved as expected.
Please, help
source to share
Your two options are either add using Base::Start
for scope resolutionStart
int Derived::Start(bool b, MyType *mType)
{
using Base::Start;
m_mType = mType;
return Start(b);
}
Or, as you noted, add a prefix Base::
.
int Derived::Start(bool b, MyType *mType)
{
m_mType = mType;
return Base::Start(b);
}
source to share
This is due to the fact that the name is being hidden.
When you declare a function in a derived class with the same name as the base class, the base class versions are hidden and not accessible through an unqualified call.
You have two options, either fully qualify your call like Base::Start(b)
, or place a declaration using
in your class:
using Base::Start;
source to share