FAQ 21.09 Should the scope operator :: be used when invoking virtual member functions?

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 } 

(1) Legal but dubious

(2) Generally this is better for nonstatic member functions called from user code



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net