Re: Virtual functions
On Jul 16, 11:56 am, paul <pradhan.push...@gmail.com> wrote:
I was trying out the following sample code from Scotty Meyers:
Effective C++ book:
#include <iostream>
class B {
public:
virtual void f() const { std::cout << "B::f()" << std::en=
dl; }
There is a 'const' on that line.
};
class D : public B {
public:
virtual void f() { std::cout << "D::f()" << std::endl; }
No 'const' on that line. Two compilers warn about that "name hiding": g
++, and Comeau online test compiler.
};
int main()
{
D dx;
B *pb = &dx;
pb->f(); // will call B::f()
B has only one f() and D does not override it, so B::f() should be
called.
return 0;
}
He says according to the standards this should be a compiler error but
some compilers allow this to work.
I can't see how it is a compiler error.
I found Visual Studio and g++ 3.2
allow this.
I would like to know if this is true, if not what maybe the philosophy
behind allowing this ambigious behavior?
I think name hiding was introduced to prevent certain types of hard to
detect errors. Without name hiding, a function in D might be in use
today through automatic type conversions on its parameter(s); but
then, introducing a function in B with the same name but with a better
matching parameter(s) could silently start using that function
instead, even though we're using D's interface. (Something like
that... :) )
Ali