Recently one of my friend asked me how to prevent class inheritance in C++. He wanted the compilation to fail.
I was thinking about it and found 3 answers. Not sure which is the best one.
1) Private Constructor(s)
class CBase
{
public:
 static CBase* CreateInstance() 
 { 
  CBase* b1 = new CBase();
  return b1;
 }
private:
 CBase() { }
 CBase(CBase3) { }
 CBase& operator=(CBase&) { }
};
2) Using CSealed base class, private ctor & virtual inheritance
class CSealed
{
private:
 CSealed() {
 }
 friend class CBase;
};
class CBase : virtual CSealed
{
public:
 CBase() {
 }
};
3) Using a CSealed base class, protected ctor & virtual inheritance
class CSealed
{
protected:
 CSealed() {
 }
};
class CBase : virtual CSealed
{
public:
 CBase() {
 }
};
All the above methods make sure that CBase class cannot be inherited further.
My Question is:
- Which is the best method ? Any other methods available ? 
- Method 2 & 3 will not work unless the - CSealedclass is inherited virutally. Why is that ? Does it have anything to do with vdisp ptr ??
PS:
The above program was compiled in MS C++ compiler (Visual Studio). reference : http://www.codeguru.com/forum/archive/index.php/t-321146.html
 
     
     
     
     
     
     
     
     
     
     
     
    