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: C++
Expertise: Advanced
Sep 5, 2000

Disabling Further Inheritance of a Class

To block a class from being derived any further, declare its constructor private. Next, add a public static member function Instance() that creates an instance of this class and returns its address. Calling Instance() is the only way to create instances of this class because the constructor is otherwise inaccessible. Here's an example:

 
class A
{
private:
 A(); // private constructor
public:
  static A* Instance(){ return new A;}
};

main()
{
 A *pa = A::Instance();
 // use pa
 delete pa;
}
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date