Constructors are defined as private or protected benefits _c language

Source: Internet
Author: User

How does an object create a constructor, a destructor that is declared private and protected? The constructor cannot be called from the outside, but the object must be constructed, how it should be resolved, please help us explain, about the construction, the use of destructors declared as private and protected??? Ask this question, which means you have thought about C + +.

Syntactically, a function is declared as protected or private, and the function cannot be invoked directly from the outside.
For protected functions, other functions of the subclass's "inner" can be invoked.
For private functions, it can only be called by other functions of the "internal" of this class.

That's the way it's grammatically prescribed, and you know it.
So why do you sometimes declare constructors or destructors as protected or private?

The following scenarios are commonly used:
1. If you don't want the outside user to directly construct a class (assuming that the class's name is a) object, if you want the user to construct only subclasses of this class A, you can declare the constructor/destructor of Class A as protected, and declare the constructor/destructor of Class A subclasses as public. For example:

Copy Code code as follows:

Class A
{protected:a () {}
Public: .....
};
CALSS B:public A
{public:b () {}
....
};

A A; Error
b b; Ok


2. If you declare a constructor/destructor private, then only the "internal" functions of the class can construct objects of this class. The "inside" here is not sure if you can understand it, let's give an example.
Copy Code code as follows:

Class A
{
Private
A () {}
~a () {}

Public
A function within the void Instance ()//Class A
{
A A;
}
};


The code above can be compiled. The instance function in the code above is a function of the interior of Class A. The object of a A is constructed in the instance function body.

However, this instance function is still not able to be called outside. Why, then?

If you want to call the instance function, you must have an object constructed. However, the constructor is declared private. External cannot directly construct an object.
A Aobj; Compile Pass
Aobj.instance ();
However, if instance is static, it can be invoked without the need for an object. As follows:

Copy Code code as follows:

Class A
{
Private
A ():d ATA (a) {cout << "A" << Endl;}
~a () {cout << "~a" << Endl;}

Public
Static a& Instance ()
{
Static a A;
return A;
}

void Print ()
{
cout << data << Endl;
}

Private
int data;
};

a& RA = a::instance ();
Ra. Print ();


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

In another case, the copy constructor and the Operator= (assignment operator overload) are usually declared private, but no body is implemented.
The purpose of this is to prevent the external users of a class from copying the objects of the class.
For details, see clause 27 in effective C + +.

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.