Destructors are a bit fuzzy when calling default destructors and user-written destructors themselves. Write section code Summary under
[CPP]View PlainCopy
- #include <iostream>
- Using namespace std;
- Class Box
- {
- Private
- double length;
- Public
- Box (double lv=1.0): Length (LV)//constructor has no return value
- {
- cout << "constructor called" << Endl;
- }
- ~box ()//rewrite destructors (Overrides are re-constructs of the base class for inheriting classes, which are not expressed here)
- {
- cout << "destructor called" << Endl;
- }
- }; //The vicious semicolon, I always forget
1, First Direct declaration ( definition) look down
Many friends pointed out that I do not use the statement here, on the 11 floor made a certain explanation, specifically not here to repeat, here to "definition", thank you for correcting, but I also keep my own opinion, so did not put "statement" removed
[CPP]View PlainCopy
- int main ()
- {
- Box Box (2.3);
- }
Here's a little bit different from Java and C + +, where C + + creates an object at the time of declaration, and the Java declaration simply creates a reference and does not allocate memory. To get to the point, explain that the constructor was called after the declaration and then exited when the destructor was called.
2. Declaration pointers
[CPP]View PlainCopy
- int main ()
- {
- Box *box;
- }
As you can see, declaring a pointer does not call the constructor, nor does it allocate memory space.
3. Create with new
[CPP]View PlainCopy
- int main ()
- {
- Box *box=new Box (2.3);
- }
Simply call the constructor to create the object and allocate the memory space. However, the destructor is not called because the memory of the object specified by box is created by new and the compiler cannot automatically call the destructor to delete it. So you need to call Delete before you can.
4. Create the object with new and delete
[CPP]View PlainCopy
- int main ()
- {
- Box *box=new Box ();
- Delete box;
- }
This time the destructor is called. As can be seen, the destructor at this time is not the compiler itself calls, is by our program to invoke the initiative, so we need to pay attention to later. New needs to manually free up memory space
5. When do I need to rewrite destructors?
[CPP]View PlainCopy
- Class Message ()
- {
- Private
- Char *message;
- Public
- Message (const char* text="default message")
- {
- message = new char[strlen (text) +1];
- strcpy (message, text);
- }
- void Showit ()
- {
- cout << "message:" << message << Endl;
- }
- ~message ()
- };
- Message::~message ()
- {
- cout << "destructor called" << Endl;
- Delete [] message;
- }
As you can see from the example, when you call new in your constructor to create an object's memory allocation space, you need to call delete specifically to free memory, so you need to override the destructor to specifically free this memory space
6.
- Using namespace std;
- Class Box
- {
- Private
- double length;
- Public
- Box (double lv=1.0): Length (LV)//constructor has no return value
- {
- cout << "constructor called" << Endl;
- }
- ~box ()
- {
- cout << "destructor called" << Endl;
- }
- void Showit ()
- {
- cout << this->length << Endl;
- }
- };
- void Display (box box)//Key Note this place ......... ........
- {
- Box.showit ();
- }
- int main ()
- {
- Box box;
- Display (box);
- }
Run results
If you change the display code above to
[CPP]View PlainCopy
- void display (Box &box)//change to call is a reference
- {
- Box.showit ();
- }
Run results
It is obvious to see that there are two destructors that are called when you do not add a reference . Why is it?
A direct pass is a parameter transfer, so an additional object is created to copy the Object box in the main function, so the destructor is called at the completion of the display call to release the memory space of the parameter object created by the function. But if the reference is passed, there is only one parameter object, so only one is called.
If it is the basic type, you should understand, directly to the box in main the value of copy to the formal parameter box is, but to the object here is a bit complex, if it is simple like the above example of the double type, and its own copy function can be copied to the Parameter object , but if there is a reference, such as char *pp = new char[100], then only the address is copied, two objects common one address, it is possible to cause errors. So you need to pay attention to this later, the calling object needs to use the reference oh ... (Or you could write another copy function yourself.) )
[Reprint]c++ destructor invocation