Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language:
Expertise: Beginner
Nov 29, 2001

When to Declare a Virtual Member Function Private


Declaring a virtual member function private may be useful when you wish to limit the number of derived classes that can override the given function. In such a case, you declare all the derived classes, which will be able to override the private virtual member function as friends of the base class.

In the following example, only B can override the member function f().
 
class A {
  friend class B;
private:
  virtual int f() { return 1; }
};

class B : public A {
private:
  virtual int f() { return 2; }
};

class C : public A { // will use A::f(), cannot _
override it
};

class D : public B { // will use B::f(), cannot _
override it
};
Ivan Gerasimov
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap