In the C ++ constructor, destructor declared as private, protected

Source: https: //zhidao.baidu.com/question/151143551.html

Q: I have seen some programs, constructors , destructors sound out private insurance and protected, then the object how to create? Has been unable to call from outside the structure constructors, but the object will have to be constructed, how to solve big trouble at home to help explain, with regard to construction, the destructor is declared when the usage of private and protected, thank you

Your question to show that you've been thinking for a c ++. 

Grammatically speaking, a function is declared as protected or private, then this function is not from "outside" to be called directly.
For functions protected, the "inside" subclass of the other functions can be called.
For a private function, this class can only be "inside" the other said function call.

Grammar is so specified, you must also know slightly.
So why sometimes the constructor or destructor is declared as a protected or private?

Scene generally used as follows:
1. If you do not want outside users to directly construct a class (assuming the name of the class is A) of the object, the user can only hope to construct this subclass of class A, then you can be the constructor of the class A / destructor declared as protected, and the constructor subclass of class a / destructor declared as public. For example:
class A
{protected: A () {}
public: ....
};
calss B: A public
{public: B () {}
....
};

A A; // error
B B; // the ok

2. If the constructor / destructor is declared as private, it can only "inside" of this class of functions to construct objects of this class. Here the word "internal" do not know if you can understand, the following give you an example.
A class
{
Private:
A () {}
A ~ () {}

public:
an internal function of class A // Instance void ()
{
A A;
}
};
above code is compiled through. Instance functions of the above code is a function of the class A inside. Instance function body to construct an object A is.
However, this function is still Instance can not be called out. why?
If you are calling Instance function, it must have an object to be constructed. But the constructor is declared private in the. External can not directly construct an object out.
A aObj; // compiler pass
aObj.Instance ();
however, if Instance is a static static function, then it may not be required by an object, and can be called directly. As follows: class A
{
Private:
A (): Data (10) {COUT << "A" << endl;}
~ A () {COUT << "~ A" << endl;}

public:
static A & Instance ( )
{
static A A;
return A;
}

void the Print ()
{
<< endl << Data COUT;
}

Private:
int Data;
};

A & RA = A :: Instance ();
ra.Print ();

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

There is a case where: generally copy constructor and operator = (assignment operator overloading) declared as private, but not implemented thereof.
The purpose of this is to prohibit a user outside a class of objects of this class copy operation.
See details "as Effective C ++" an article inside. Which specific terms do not remember. You find it.

Guess you like

Origin blog.csdn.net/alss1923/article/details/78981990