Wednesday, November 3, 2010

C++ Series - Interview questions

How to prevent a class from inheritance ?

) 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() { } 
}; 

No comments:

Post a Comment