Virtual constructor is not allowed in C ++, if virtual is added before the constructor.
Take vc6 as an example. The Compiler reports the following error: 'line' is the only legal storage class for constructors.
Although I don't understand the relationship between this and inline, the error is positive.
Constructor and destructor can both be private. In this case, the constructor is compiled, but the class cannot be instantiated, even if it is inherited.
For example:
Class
{
PRIVATE:
A ();
Virtual ~ A ();
};
Class B: public
{
Public:
B ();
Virtual ~ B ();
};
B;
AboveCodeIs not compiled, because B (),~ B () respectively needs to call (),~ A (), and they are all private, so they cannot be called.
The solution is only friend. Includes the friend class and friend function. For example, you can modify the definition of a as follows to implement
Class
{
PRIVATE:
A ();
Virtual ~ A ();
Friend Class B;
};
Or the friend function can generate a variable internally, because it can call a's private constructor and destructor.
Transferred from:Http://blog.myspace.cn/e/400067551.htm