Female
---- Construction destructor and life and death
Author: HolyFire
In "by the beginning to end - constructor", I mentioned that the constructor and the destructive function in C and the life of the object and the life of the object are very good, and it can control the life and death of the object. Programming ideas is closer to the law of the real world.
Let's recall it. When you create it, the appropriate constructor will be automatically called, while the object is destroyed, the destruction function will be automatically called. In the case where the constructor and the destructuring function can be called, it is also declared as public, there is no problem, but once we change their visibility to private and protected, then the situation will Change.
When declaring as private, then in addition to the class itself can be called, there is also an object (typically class or function) that the Friend keyword gives a trust relationship (typically a class or function) can be accessed.
#include
Using namespace std;
Class a {
PUBLIC:
Static A * Createa (Void)
{
COUT << "Create a a instance by class a's member function" << endl;
Return new a;
}
Private:
A () {}
Friend A * Createa (Void);
}
A * Createa (Void)
{
cout << "Create a A instance By class A's friend Function" << endl;
Return new a;
}
void main ()
{
A * a1 = a :: cretea ();
A * a2 = cretea ();
Delete A1;
Delete A2;
cin.get ();
}
turn out:
Create a a a instance by class a's member function
Create a a a instance by class a's friend function
If you use A a in this way, it is unable to create a instance, the compiler will tell you that it will not be busy.
Of course, declare the destructive function is private or restricting the conditions destroyed by the instance of A, then
Delete A1;
Delete A2;
It is also protest of the compiler, we need to write another function or class to destroy them.
There are a lot of things in real life. For example, there is only one sun in the solar system. If there are two sun going to make a mess, it is really unimaginable. When you create a conditional limit, we can write a document to tell others to pay attention to the program, but as the project is increasing, don't say others, even if you can forget, it is very strict on the condition of conditional restrictions. Our knowledge will send it.
Combined with the content that the constructive function declares as a private content and "unique ---- static member variable", we can design a type that can only have an instance. Below is the source code of C , in this example, the destruction of the instance is also limited.
#include
Using namespace std;
Class a {
Private:
Static a * a; // A static member variable, all instances of the class
A () {cout << "this is class a, i couldnow't direct creat instance" << Endl;} ~ a () {cout << "this is class a, i coudn't direct destroy instance" << Endl;
PUBLIC:
Void showinstance () {cout << this << endl;}
Friend A * CreatObjectofa (Void); // Entrust the trust relationship, because the class does not provide a method of creating an instance, then this task is handed over to this function.
Friend Void DestroyObjectofa (A * a); // Correspondingly, this function is responsible for destroying the instance
}
A * a :: a = null;
A * CreatObjectofa (Void)
{
If (a :: a == null) // has an instance of Creating a
{//No
A :: a = new a;
Cout << "this is a class a instance creat by CreatObjectofa function." <<
} // If you created, you don't have to create it.
Return A :: A; // Since A :: A is a static member variable, then the return is the same pointer
}
Void DestroyObjectofa (A * _A)
{
If (a :: a! = null) // Cord Create, if there is an instance being created, then destroy it
{
Delete a :: a;
A :: a = NULL;
Cout << "this is a class a instance destroy by destroyObjectofa function." << endl;
}
_a = NULL;
}
void main ()
{
A * A1, * A2, * A3;
A1 = CREATOBJECTOFA ();
A2 = CREATOBJECTOFA ();
A3 = CREATOBJECTOFA ();
A1-> Showinstance ();
A2-> Showinstance ();
A3-> Showinstance ();
DESTROYOBJECTOFA (A1);
DESTROYOBJECTOFA (A2);
DESTROYOBJECTOFA (A3);
cin.get ();
}
turn out:
This is class a, i couldn't direct creat instance
This is a class a instance creat by CreatObjectofa function.
00864898 // Point to the same place
00864898
00864898
This is class a, i coudn't direct destroy instance
This is a class a instance destroy by destroyObjectofa function.
Only one instance is created and destroyed until the end
No matter how many times you created, this class has a maximum of one, which is exactly what we want. As long as you want to think, there are a lot of interesting things waiting for you.
State the constructor with the destructor as protected, then our purpose is that this class will be used as the base class, and this base class cannot be directly instantiated. Why do you want to do this, in fact, we have some incomplete pieces while understanding things and abstract things for a lot of things, and their separate appearance is not suitable. For example, some simple machined semi-finished products, even in reality, their semantics are incomplete. There are also completely abstraction, such as: "people", not specific to men, women, children, adults, will not consider it with realistic thoughts. So, use a C example to implement it, master it.
#include
Using namespace std;
Class a {
protected:
A () {cout << "this is class a, i couldn't direct creat instance" << endl;}
}
Class B: Public a {
PUBLIC:
B () {coup << "this is class b public inherit from class a, i could create instance by class b" << endl;}
}
void main ()
{
B B;
cin.get ();
}
turn out:
This is class a, i couldn't direct creat instance
This Is Class B Public Inherit from Class A, I Cer Creat Instance by Class B
This example is very simple, we don't need to spend how much time can be understood.
It seems to be a bit suddenly until now, but there is no above explanation, it will be a bit difficult to understand, so nonsense, we directly cut into the theme.
In the old legend of China, people are done with mud, so people can't create casually, can create people's things can be a woman.
The woman has changed from men and women, and women can have children, that is, they can create people, but only men can make women children, so the relationship is that to create a man must have a man to let a woman, have children, have a child The behavior is a woman, but the initiative is the behavior of men with the right to use women, this is a man's behavior. We can understand that men's behavior is a message to tell women, women use corresponding behavior.
Let's sort out: (I use a simplified mode to consider the problem, point to clarify the relationship between the constructor for the life of the object)
Men and women are people, women are born, children are also people, so children should be divided into men and women, then women's children are in fact two behaviors, born boys, born girls, men have two behaviors, let women live Boy makes a girl girl.
Female 娲 {Creating a man, creating a woman}
people{}
Man {people, let a woman boy (man), let a woman girl (woman)}
Woman {people, born boys (men), girls (woman)}
Class Person {///
protected:
Person () {} // Constructor can make the derived class, that is, men and women
}
Class Man; Class Woman;
Class nvwa {// female
PUBLIC:
Man * creataman (void); // Creating a man
Woman * creatawoman (void); // Creating a woman
}
Class Woman: virtual public persourn {// woman
Private:
Woman () {}
Man * creataman (void); // born boys (men)
Woman * Creatawoman (Void); // Girl (Woman)
Friend nvwa;
Friend man;
}
Class Man: virtual public persourn {// man
Private:
MAN () {}
PUBLIC:
Man * getaboy (Woman * Woman) // Let a woman boy (men)
{
Return Woman-> Creataman ();
}
Woman * getagirl (Woman * Woman) // Let a woman girl (woman)
{
Return Woman-> Creatawoman ();
}
Friend nvwa;
Friend Woman;
}
Man * nvwa :: Creataman (void)
{
Return new man;
}
Woman * nvwa :: CreataWoman (void)
{
Return New Woman;
}
Man * Woman :: Creataman (void)
{
Return new man;
}
Woman * Woman :: CreataWoman (Void)
{
Return New Woman;
}
void main ()
{
NVWA nvwa; // female
MAN * man = nvwa.creataman (); // created a man
Woman * Woman = nvwa.creatawoman (); // created a woman
Man * boy = man-> getaboy (Woman); // man makes a woman gave birth to a boy
Woman * girl = man-> getagirl (woman); // let the woman have a girl
Delete man;
Delete Woman;
Delete boy;
Delete girl;
}
In fact, a relaxed friend is not difficult to find that creators can abstain becomes a behavior, men and women are people, but do not know that boy or girl, because the characteristics of the inheritance cannot be learned by its base class Information, this problem also has a solution in C , I will introduce in future articles.
2001/9/18
Ding Ning