Privte destructor and constructor

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.