inherited member function not seen

D

Daniel Barna

Hi
I am a bit confused. I thought always that if a class inherits from
another (public), then all of its base class's public member function
are also visible in this class - even if it declares a function with
the same name, but different parameters.

However, when I try to compile the example below, the compiler claims
that there is no appropriate function:
invalid conversion from 'int' to 'const char *'
initializing argument 1 of 'void B::push_back(const char *)'

However, if I don't declare a 'push_back' function inside 'class B',
then everything works fine. Should the compiler in this case not
resolve the
'push_back' function (based on its argument type: int) to
A::push_back(int) ?

Thank you
DAniel

-----
class A
{ public:
void push_back(int) {}
};

class B : public A
{ public:
void push_back(const char *) {}
};

int main()
{
B b;
b.push_back(1);
return 0;
}
 
R

Rolf Magnus

Daniel said:
Hi
I am a bit confused. I thought always that if a class inherits from
another (public), then all of its base class's public member function
are also visible in this class - even if it declares a function with
the same name, but different parameters.

If you declare a function in a derived class, _all_ functions of its
base classes with the same name are hidden.
However, when I try to compile the example below, the compiler claims
that there is no appropriate function:
invalid conversion from 'int' to 'const char *'
initializing argument 1 of 'void B::push_back(const char *)'

However, if I don't declare a 'push_back' function inside 'class B',
then everything works fine. Should the compiler in this case not
resolve the 'push_back' function (based on its argument type: int) to
A::push_back(int) ?

No. But you can explicitly make it visible in B. See my addition to your
code.
Thank you
DAniel

-----
class A
{ public:
void push_back(int) {}
};

class B : public A
{ public:
void push_back(const char *) {}

using A::push_back;
 
S

Sharad Kala

Daniel Barna said:
Hi
I am a bit confused. I thought always that if a class inherits from
another (public), then all of its base class's public member function
are also visible in this class - even if it declares a function with
the same name, but different parameters.
No, you got it wrong. Base class member functions with same name as a
derived class member function are hidden in the derived class.
Should be in the FAQ - http://www.parashift.com/c++-faq-lite
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top