FAQ 33.12 When should a nonfinal virtual function be invoked with a fully qualified name?

Almost never. The one situation where full qualification is appropriate and useful is where a derived class needs to invoke a member function from a base class. In this case the derived class uses full qualification ("::") to make sure the base class member is invoked, particularly in cases when the member function has been overridden in a derived class.

However, normal user code should avoid full qualification. That is, when x is a Fred& and f() is a virtual member function of class Fred, normal user code should use x.f() rather than x.Fred::f(). This is because full qualification subverts dynamic binding. If the actual class of the referent is something derived from Fred, the wrong member function may be invoked. An example follows.

 class Shape { public:   virtual void draw() throw() = 0;   virtual void hide() throw() = 0;   virtual ~Shape() throw(); }; Shape::~Shape() throw() { } class Circle : public Shape { public:   /*final*/ void draw() throw();   virtual void hide() throw(); }; void sample(Circle& c) throw() {   c.Circle::draw();                                  <-- 1   c.Circle::hide();                                  <-- 2   c.hide();                                          <-- 3 } 

(1) GOOD: Circle::draw() is a final

(2) EVIL: Subverts dynamic binding

(3) GOOD: Always calls the right hide() implementation

In sample(Circle& c), if c actually refers to a derived class of Circle that overrides the hide() member function, the call to c.Circle::hide() invokes the wrong member function.

 class FancyCircle : public Circle { public:   virtual void hide() throw(); }; int main() {   FancyCircle x;   sample(x); } 


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