Re: Why can't call a base member function from a object of sub class???
* Albright:
Code as bellow:
#include <stdio.h>
#include <string.h>
class A
{
public:
void print(char *s)
{
printf("%s\n", s);
}
};
class B : public A
{
public:
void print(int i)
{
printf("%d\n", i);
}
};
int main()
{
B b;
b.print("hello"); //Has complile error here, it indicates that b
invoke B::print() but not A::print, I want to know why.
return 0;
}
In this sample, if print(char *s) is defined in class B, it's OK, but
if in A, it's not.
Why these two functions are NOT overloaded between base class and sub
class?
This is a FAQ (Frequently Asked Question).
See the FAQ item titled "What's the meaning of, Warning: Derived::f(char) hides
Base::f(double)?", currently item 23.9 and available at e.g. <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9>, plus
at a host of mirror sites.
It's often a good idea to check the FAQ.
Cheers & hth.,
- Alf