I see an article about this issue. There is a code:
Code:
class A
{
public:
virtual void foo(int n = 3)
{
std::cout << "A::foo(" << n << ")" << endl;
}
};
class B : public A
{
public:
virtual void foo()
{ std::cout << "B::foo" << endl;
}
};
int main()
{
A* a = new B; a->foo(); // prints A::foo("3");
}
Also i tried without virtual keyword, it also gives same result. But i don't understand why it is so.
Code:
class A
{
public:
void foo(int n = 3)
{
std::cout << "A::foo(" << n << ")" << endl;
}
};
class B : public A
{
public:
void foo()
{ std::cout << "B::foo" << endl;
}
};
int main()
{
A* a = new B; a->foo();
A as;
as.foo();
}
How can A see the foo() function?
I can call as.foo() not as.foo(3); it doesnt give me error but it prints A::foo("3") but i didnt call a.foo(int n = 3);
What does compiler do for such a situation when i inherite a class.
A can see foo() function; is this normal? Because i know that function name resolution is only legal for scope. So A mustnt see foo() function. But it sees.
Can anyone please explain this.
Thanks.