In a derived class, the signature of the function is as follows:
virtual void foo();
which does not mention const. Its non-constant member function, and Base::foois a const member function . These are two different functions, because it constis part of the function signature.
virtual void foo() const;
, .
, :
class Derived : public Base {
public:
virtual void foo() const {
cout << "Derived::foo()" << endl;
}
};
As const . , foo.
@davka :
, const ? - ?
obj Base, . Base . , . Base .
void func(Base& obj) {
obj.foo();
}
, :
void func(Derived & obj) {
obj.foo();
}
, Base::foo Derived.
Derived::foo Base::foo, , Derived.
Base::foo .
class Derived : public Base {
public:
using Base::foo;
virtual void foo() {
cout << "Derived::foo()" << endl;
}
};
Derived (const, ) , . .
Derived unhidden, ?
void f(Derived& obj) {
obj.foo(); //Which function? Base::foo or Derived::foo?
}
void g(const Derived & obj) {
obj.foo(); //Which function? Base::foo or Derived::foo?
}
Derived::foo, , Base::foo, const. : const, , , , .
-: http://www.ideone.com/955aY