from:http://blog.csdn.net/lazy_tiger/article/details/2224899
A class cannot be inherited, that is, its subclasses cannot construct a parent class, and there is no way for a class to instantiate an entire subclass so that subclasses cannot inherit the parent class. We can declare a class's constructor as private, so that the class's constructor is not visible to the subclass, and the class cannot inherit. However, this begs the question, is it not possible for client programs to instantiate this class? OK, let's refer to the singleton mode, with a static function to help create an instance of this class, the problem is solved!
class cparent
{
Private:
cparent (int  V) {m_v = v;}
~cparent () {}
int m_v;
static CParent * m_instance;
Public:
Void fun () {cout << "the value is: " << m_v << endl;}
static cparent * getinstance (INT&NBSP;V);
};
cparent * cparent::m_instance = null;
Cparent * cparent::getinstance (int v)
{
m_instance = new cparent (v);
return m_instance;
}
This is a valid method, but the instance created by the static function must be static. Moreover, this class cannot build objects like normal classes, such as:
Cparent C; Impossible
Think of a different way of thinking, friends can not be inherited? We can define a friend function to help construct an instance of the class while defining the class's constructor as private.
Class Cparent
{
Private
cparent (int v) {m_v = V;}
~cparent ();
int m_v;
Public
void Fun () {cout << ' the value is: ' << m_v << Endl;}
Friend Cparent * getinstance (int v);
};
Cparent * getinstance (int v)
{
Cparent * pinstance = new Cparent (v);
return pinstance;
}
This class is also not inherited, but the problem arises as before: we cannot treat this class as we do to ordinary classes.
Now imagine that there is a cparent class, and we don't want him to be inherited. Under the guidance of friends cannot be inherited, we should consider let cparent's friend attribute cannot be inherited. Suppose there is an auxiliary class cnoheritance,cparent is a friend of cnoheritance class. Also assume a cchild class, which attempts to inherit cparent class (if it has this ability).
The constructor of the Cnoheritance class is defined as private, and Cparent is declared as a friend of the Cnoheritance class. At the same time Cparent inherits the Cnoheritance class. So far,cnoheritance has been unable to access and instantiate it except for the Cparent class. Cchild because the friend attribute of the cparent cannot be inherited, Cchild cannot instantiate the Cnoheritance directly (but can pass cparent).
So far, cparent can still be inherited. This is a trick. Let's tidy up the idea, the following illustration shows the relationship between Cnoheritance, Cparent and cchild three classes.
If we let cparent class virtual inheritance cnoheritance class, based on the characteristics of virtual inheritance, the constructor of the virtual base class is constructed by the final subclass. So cchild if you want to inherit cparent, it must be able to construct cnoheritance, which is impossible! Therefore, our cparent has finally become a class that cannot be inherited.
Class Cnoheritance
{
Private
Cnoheritance () {}
~cnoheritance () {}
Friend class Cparent;
};
Class Cparent:virtual Public cnoheritance
{
Public
cparent (int v) {m_v = V;}
~cparent () {};
Private
int m_v;
Public
void Fun () {cout << ' the value is: ' << m_v << Endl;}
};
Class Cchild:public Cparent
{
Public
Cchild (): Cparent (10) {}
~cchild () {}
};
It is important to note that the Cnoheritance class we are introducing here has introduced only private constructors and destructors to the entire program, so it will not bring ambiguity because of possible diamond inheritance.
[Go] Implements a C + + class that cannot be inherited