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 constructorpublic:  static A* Instance(){ return new A;}};main(){ A *pa = A::Instance(); // use pa delete pa;}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: