Say now I have been accustomed to this life, before I was not sleep at noon will die star people, now feel not to sleep is that, ready to put their time well arranged, because I was in the training course, so the time is also very tight, the previous plan to update the blog every night, it seems to be realized. I decided to review the contents of the day before noon and to consolidate what I learned in the morning. I think this should be the most efficient way for me to learn. In this thank you for the struggle of the little song Thank you for your encouragement, I think this will be my future perseverance is the motivation, I will insist that is not 6 months? Come on!!
Okay, nonsense, let's go into today's theme!
destructor: is used to dispose of objects. In C + +, everything is by object, but there are member variables, member functions and the like, so the destructor is to release the memory space occupied by the object.
Features of the destructor: no return value, cannot be overloaded, must be public, without any parameters
So, when will the destructor be used? Summary sentence is the object immediately release time!
1. Scope of the Left object
2. Destroying objects
3. When you delete a pointer to an object
#include <iostream>using namespace Std;class test{public:test () {cout<< "This is the default constructor without parameters" <<ENDL; ~tes T () {cout << "This is a destructor"}};void main () {Test t;return;} /******************************************/the output is this is the default constructor with no parameters this is a destructor
Shallow copy: An object of a class with an over assignment operation can copy itself to another object of the same class. But this assignment is a mutual assignment between object data members, which may create just a physical copy
This shallow copy causes two deletions when the copied object contains a pointer member variable, which is illegal. So this copy should be avoided
Class Thing {Private:int A, B; int *ppublic:thing (): B (0) {a=0,p=&a}}; Thing t1,t2;t1=t2;
In this program section, I declare a class Thing, which contains a pointer-type member variable, int *p, and then I assign the T2 of the Thing type to T1, this time the value of the T2 pointer is paid to T1, and the pointer points to the T2 member variable A, when the memory is freed, Release T2 first, and then release T1, found that the T1 pointer variable points to the memory has been freed, so will throw an exception, that is, two times delete.
Of course, in this program the constructor method initializes the data member with an initialization list, which is more efficient than assigning it to the function.
In a class, the Access function get and print functions are typically used as const member functions, constant member functions, and cannot modify the value of a member variable.
This pointer
Each object contains a pointer to itself, called the This pointer. He is hidden, but can be called explicitly where it is needed.
When using pointers, we should pay attention to the precedence of operators, generally we want to use them like (*this)
The chain expression for this pointer:
class thing { public: thing & setx (Int a ) { (*this) .x=a; return *this; } thing & sety (int b) { (*this) .y=b; return *this; } thing & Setz (int c) { (*this). Z =c; return *this; } }; Thing thing ; thing.setx (1). Sety (2). Setz (3); /******************************** / output after printout thiThe value of the xyz of NG: 1 2 3
What happens if the reference is removed?
Removing the reference function returns a temporary object, and he will pay 2 3 to the newly generated two temporary objects, so the result of the removal of the reference cannot be predicted, so we must avoid this situation.
The features of this pointer:
1. This pointer is created automatically by the compiler in the Declaration class object (does not need to be defined by itself)
2, each object or function (member variable, or function non-static all hides a this pointer)
3. This pointer retains the address that points to the object
4. When a non-static member function of a class accesses another non-static function, the compiler automatically passes the address of the object to the internally called function. (That is, when another member function is called within a member function, the address of the object is not used)
This article is from the "Simon Eat Cattle" blog, please be sure to keep this source http://ximenchiniu.blog.51cto.com/9503623/1682214
After dinner, continue to code word. C + + destructor value, this pointer