Reason why C ++ constructor/destructor is set to private

Source: Internet
Author: User

How can I create an object if I declare the constructor and destructor as private and protected?
The constructor cannot be called from the outside, but the object must be constructed. How can this problem be solved, when the Destructor is declared as private and protected ???

If you raise this question, you have already thought about C ++.

In terms of syntax, a function is declared as protected or private, so this function cannot be called directly from the "external.
For protected functions, other functions of the subclass can be called.
Private functions can only be called by other functions in this class.

You must know the syntax.
So why are constructors or destructor declared as protected or private sometimes?

The common scenarios are as follows:
1. If you do not want external users to directly construct an object of A Class (assuming the class name is a), but want users to only construct a subclass of Class, then you can declare the constructor/destructor of Class A as protected, and the constructor/destructor of the subclass of Class A as public. For example:
Class
{Protected: (){}
Public :....
};
Calss B: public
{Public: B (){}
....
};

A A; // Error
B; // OK

2. If you declare the constructor/destructor as private, only the "internal" function of this class can construct the object of this class. The "internal" mentioned here does not know whether you can understand it. The following is an example.
Class
{
PRIVATE:
A (){}
~ A (){}

Public:
Void instance () // a function inside Class
{
A;
}
};
The above code can be compiled. The instance function in the code above is an internal function of Class. An object of A is constructed in the instance function body.
However, this instance function cannot be called outside. Why?
To call the instance function, an object must be constructed. However, the constructor is declared as private. An object cannot be constructed externally.
A aobj; // compilation and translation fail
Aobj. instance ();
However, if the instance is a static function, it can be called directly without passing through an object. As follows: Class
{
PRIVATE:
A (): Data (10) {cout <"A" <Endl ;}
~ A () {cout <"~ A "<Endl ;}

Public:
Static A & instance ()
{
Static;
Return;
}

Void print ()
{
Cout <data <Endl;
}

PRIVATE:
Int data;
};

A & RA = A: instance ();
RA. Print ();

The above code is actually a simple C ++ code implementation in the singleton mode of the design mode.

Another case is that the copy constructor and operator = (the value assignment operator is overloaded) are declared as private, but no entity is implemented.
This is intended to prevent external users of a class from copying objects of this class.

Related Article

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.