Simple, make all constructors of the class private.
I think above answer is not appropriate If you make all constructor private u cant create the obj of that class but that class can be inherited in to another class and methods of that base class can be accessed by using derived class object. Isn't it?
Before creating a non-inheritable class crate a temorary class whose constructor or destructors are private.Then make your no-inheritable class as friend to this temporary class.Now make the temopoaryu class as virtual base class of your non-inheritable class. Example: class Temp { private: ~Temp() { }; friend class FinalClass; };
class FinalClass : virtual public Temp { . . . };
We inherit our FinalClass virtually from Temp class and make Temp as a virtual base class. Now whenever anyone attempts to inherit class from FinalClass and make object of it, then its constructor tries to call the constructor of Temp. But the constructor of Temp is private so compiler complains about this and it gives error during the compilation, because your derived class is not friend of Temp. Remember friendship is not inherited in the derived class. After all your parent's friends are not your friends and your friends are not friends of your children. But when you create object of FinalClass then its constructor can call the Temp?s constructor because FinalClass is friend of Temp.
friend class and virtual inheritence did not pass the test (I can explain why) Solution: class Final { private: Final(){} public: static Final* GetFinal() { return new Final(); } };
any attampt to inherit from this class will fail to create an object Final* f = Final::GetFinal();
Just make constructor, destructor, copy constructor, assignment operator private. so that no other class can inherit. class Base { private: Base() { } ~Base() { } Base & operator=(const Base&){} Base(const Base&) { } };