FAQ 33.13 Should full qualification be used when calling another member function of the same class?

Only if the called member function is a final member function.

If D::f() calls this->g(), full qualification (for example, this->D::g()) should be used only if D::g() is a final member function or D is a final class.

 class B { public:   virtual void f() throw() = 0;   virtual void g() throw() = 0;   virtual void h() throw() = 0;   virtual ~B(); }; class D : public B { public:   virtual void f() throw();   virtual void g() throw();   /*final*/ void h() throw(); }; void D::f() throw() {   g();                                               <-- 1   D::h();                                            <-- 2   D::g();                                            <-- 3 } 

(1) GOOD: Nonfinal member function called without full qualification

(2) GOOD: Final member function called with full qualification

(3) EVIL: Do not call nonfinal member function with full qualification!

Although it seems as if D::f() should be able to use full qualification when calling D::g(), such a thing is dangerous and invokes the wrong function in some cases. For example, if the this object were actually of a further derived class that has an override of g(), the wrong function would be invoked:

 class D2 : public D { public:   virtual void g() throw(); }; int main() {   D2 x;   x.f(); } 

Note that this is simply a specialization of the guideline presented in FAQ 33.12.



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