G.S
G.S

Reputation: 123

virtual function overriding in C++

I have a base class with virtual function - int Start(bool) In the derived there is a function with the same name but with a different signature -

int Start(bool, MyType *)

But Not virtual

In the derived Start(), I want to call the base class Start()

int Derived::Start(bool b, MyType *mType)
{
    m_mType = mType;
    return Start(b);
}

But it gives compilation error.

"Start' : function does not take 1 arguments"

However Base::Start(b) works

In C#, the above code works i.e the reference to Base is not required for resolving the call.

Externally if the call is made as follows

Derived *d = new Derived();
bool b;
d->Start(b);

It fails with the message:

Start : function does not take 1 arguments

But in C#, the same scenaio works.

As I understand the virtual mechanism cannot be used for resolving the call, because the two functions have different signature.

But the calls are not getting resolved as expected.

Please help

Upvotes: 3

Views: 179

Answers (2)

TartanLlama
TartanLlama

Reputation: 65630

This is due to name hiding.

When you declare a function in a derived class with the same name as one in the base class, the base class versions are hidden and inaccessible with an unqualified call.

You have two options: either fully qualify your call like Base::Start(b), or put a using declaration in your class:

using Base::Start;

Upvotes: 3

Cory Kramer
Cory Kramer

Reputation: 117926

Your two options are either add using Base::Start to resolve the scope of Start

int Derived::Start(bool b, MyType *mType)
{
    using Base::Start;
    m_mType = mType;
    return Start(b);
}

Or as you noted add the Base:: prefix.

int Derived::Start(bool b, MyType *mType)
{
    m_mType = mType;
    return Base::Start(b);
}

Upvotes: 4

Related Questions