FAQ 21.09 Should the scope operator :: be used when invoking virtual member functions?Only from derived classes, constructors, or destructors. The purpose of the scope operator is to bypass the dynamic binding mechanism. Because dynamic binding is so important to users, user code should generally avoid using ::. For example, the following prints Base::f() even though the object is really a Derived. #include <iostream> using namespace std; class Base { public: virtual void f() throw(); virtual ~Base() throw(); }; void Base::f() throw() { cout << "Base::f()\n"; } Base::~Base() throw() { } class Derived : public Base { public: virtual void f() throw(); }; void Derived::f() throw() { cout << "Derived::f()\n"; } int main() { Derived d; d.Base::f(); <-- 1 d.f(); <-- 2 }
|