I am reading from ebook Templates complete guide and question which i'm gonna ask might be stupid to you but..

There is section in that 9.4.2 Dependent Base Classes which i am unable to understand.

Here is the partial text from it: http://tinypaste.com/633f0

// Variation 2: 
template<typename T> 
class DD2 : public Base<T> { 
  public: 
    void f() { Base<T>::basefield = 0; } 
}; 

I need help visualizing the line (or problem domain) in text above "Care must be taken with this solution, because if the unqualified nondependent name is used to form a virtual function call, then the qualification inhibits the virtual call mechanism and the meaning of the program changes. Nonetheless, there are situations when the first variation cannot be used and this alternative is appropriate"

I understand unqualified nondependent name etc but mixing them with virtual function call is what eluding me.

有帮助吗?

解决方案

If the qualified name ( basefield ) is a virtual function, then the qualification inhibits the virtual call. It's very much the same as if you have:

struct Base {
  virtual void vCall() { }
};

struct Derived : public Base {
  virtual void vCall() { }
};

int main() {
  Derived d;
  Base* inst = &d;
  inst->Base::vCall(); // By qualifying we won't get virtual dispatch;
                       // this calls Base::vCall directly
}

其他提示

Using a qualified-identifier class-name::function() inhibits virtual-ness of function, so you should use this->function() instead.

This also works for data members: this->basefield.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top