3

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

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
G.S
  • 123
  • 3
  • 11

2 Answers2

4

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);
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Note that Base is inside nested namespaces. So i gave - A::B::C::Base::Start . With the using option, the following error is shown in the IDE - "a class-qualified name is not allowed". – G.S Jun 12 '15 at 14:57
3

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;
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
  • Using a fully qualified name looks ok when the call is from the dervied class member function. But For a call to Base::Start() from a derived class object, the second method with the using directive looks better. But this gives the error - " a fully qualified name is not allowed" – G.S Jun 12 '15 at 15:12