Http://blog.csdn.net/wufanglove/
The following code disables inheritance:
Class;
Class lock {
Friend Class;
PRIVATE:
Lock (){}
};
Class A: virtual public lock {
//...
Public:
A ()
{}
A (int t)
{}
};
Now, if you try to generate other classes from Class A, you will get compilation errors similar to the following.
Class B: public
{}; // Lock: lock': cannot access private member declared in class 'lock'
This is because all the derived classes need to call the constructor of the virtual base class. Therefore, B generated from arequires to call the constructor of the virtual base class (that is, lock ).
And the lock constructor is private, and Class B is not the friends of the lock, so this will generate a compilation error.
If we remove the virtual keyword when class A is generated, the program will be compiled successfully. This is because in non-virtual inheritance, any class can
Call the constructor in the parent class. Therefore, in non-virtual inheritance, B will call its direct parent class A constructor and a will call its direct parent class
The constructor of the class lock is valid.