visual-studio - Regarding the temporary variable destruction problem generated during initialization of c++ object array?
PHP中文网
PHP中文网 2017-05-16 13:24:00
0
3
939

The first situation:

When creating a C++ object array, if assigned through the initialization list, temporary variables will be generated, but why is the destructor not called?
The code is as follows:

#include  using namespace std; class Point { public: Point():x(0),y(0) { cout<<"Default Constructor called.\n";} Point(int xx,int yy):x(xx),y(yy) { cout<< "Constructor called.\n"; } ~Point() { cout<<"Destructor called.\n"; } void Move(int xx,int yy) { x=xx; y=yy; } private: int x,y; }; int main() { Point A[2]={Point(1,2),Point()}; cout<<"hello"<

The output result is:


Among them, Point A[2]={Point(1,2),Point()}; initializes the object array A through two temporary variables. Why is the destructor not called to destruct it? The result is not The destructors of these two temporary objects were not called.

Second case:
Display calling the constructor for each element of the array, and the destructor will be automatically called:
The code is as follows:

#include  using namespace std; class B { int x, y; public: B(); B(int i); B(int i, int j); ~B(); void Print(); }; B::B() :x(0), y(0) { cout << "Default constructor called.\n"; } B::B(int i) : x(i), y(0) { cout << "Constructor 1 called.\n"; } B::B(int i, int j) : x(i), y(j) { cout << "Constructor 2 called.\n"; } B::~B() { cout << "Destructor called.\n"; } void B::Print() { cout << "x=" << x << ", y=" << y << endl; } int main() { B *p; p = new B[3]; cout << "*******" << endl; p[0] = B(); p[1] = B(7); p[2] = B(5, 9); for (int i = 0; i < 3; i++) p[i].Print(); cout << "*******" << endl; delete[]p; }

The running results are as follows:


It can be seen that after calling the constructor initialization for each array element, the temporary variable is destructed.


In summary, both cases are temporary variables, but why one will be automatically destructed and the other will not? Please solve it! ! !

PHP中文网
PHP中文网

认证0级讲师

reply all (3)
黄舟

I checked the information again and practiced it myself to correct the error
Reason: Object assignment is to copy the values of the members of the object on the right to the object on the left, and then release the object on the right
—————— ————
Every time B() is called (with or without parameters), a temporary object is created
Case 1. The temporary object (saved in A) is not assigned a value after it is created, so there is no way to release the object on the right Process, the object in A survives until the end of main()
Case 2. Assign a value to the p array, create a temporary object and assign it to the original object. The original object (that is, the temporary object automatically created when initializing p) is only assigned, right The temporary variable on the side is destructed, and the temporary object saved in p is destructed when delete
——————————
As for why the temporary variable on the right side of A is not destructed, you can try to construct it Output this in the function, and then output the reference to the object saved in A and you will know. If there are any mistakes or do not understand, please point it out and I will correct or answer it in time

    小葫芦

    1. The lifetime of the stack space application variable will be destroyed when it exceeds the scope. This scope is in the main function.
    2. Delete goes without saying. First of all, B[3] is also a stack space, but after you reassign it, the mission is over and should be recycled as soon as possible.

      大家讲道理
      Point A[2]={Point(1,2),Point()};

      Array A is a local variable of the function, and the memory is allocated in the stack area. Point(1,2) and Point() are also local variables and are also allocated in the stack area. At this time, the compiler will usually make an optimization to reduce memory recycling and allocation.


      p = new B[3]; cout << "*******" << endl; p[0] = B(); p[1] = B(7); p[2] = B(5, 9);

      The memory space of array p is dynamically allocated and is located in the heap area. B(0), B(7), B(5, 9) are local variables allocated on the stack area.
      The memory allocation locations of the two parties are different, and the compiler cannot optimize it, so it can only destruct it honestly.

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!