-1
#include <iostream>
using namespace std;

class A
{
public:
   A()
  {
       cout<<"ctor called"<<endl;
  }

  ~A()
  {
       cout<<"Destructor called"<<endl;
  }

  A& operator=(const A &a)
  {
       cout<<"Copy assignment operator called"<<endl;
       return *this;
  }
};

int main()
{
    A a;
    A aa[2];
    aa[0] = a;
}

3 times default constructor is called; 1 time copy assignment operator is called; 3 times destructor is called.

Question: Shouldn't the destructor be called 4 times?

eteng
  • 107
  • 1
  • 4
  • 1
    Why do you think the destructor should be called the4th time? **Copy assignment** is not the same as **copy initialization** in C++. – Jason Sep 13 '22 at 03:38
  • Oh. I wonder that if A is a class with a pointer, then the old aa[0]'s memory (before the copy assignment execution) won't be released properly? (leads to memory leak?) – eteng Sep 13 '22 at 03:44
  • You assignment statement is the same as `aa[0].operator=(a);` which involves no creation of new objects. – Jason Sep 13 '22 at 03:45
  • 1
    @eteng Yes, the copy assignment operator overload is responsible for freeing any resources if the class is managing one manually. That's why we have the [rule-of-three/five](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). Or better, don't manage resources like memory allocations manually in a class and use RAII types managing the resource for you instead, e.g. a `std::unique_ptr`. Then the rule-of-zero works out. – user17732522 Sep 13 '22 at 03:47
  • Also, refer to [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jason Sep 13 '22 at 03:49
  • @eteng The way to say thanks on SO is by upvoting/accepting a post. Refer to [what should I do when someone answers my question](https://stackoverflow.com/help/someone-answers) – Kal Sep 13 '22 at 04:22

2 Answers2

3

No, the destructor should be called three times.

There are three objects of type A:

  • one variable a
  • two elements of the array variable aa

There are no other objects of type A, neither variables nor temporary objects.

Assignment does not create any objects. The line aa[0] = a; does not create any new A object. It just calls the operator= overload of A that you defined.

Since there are three objects of type A, there should be three calls to the destructor of A, one for each object of type A.

user17732522
  • 53,019
  • 2
  • 56
  • 105
1

Shouldn't the destructor be called 4 times?

No the destructor should not be called 4 times as you're only constructing the object 3 times for which you get the corresponding 3 destructor calls.

It seems that you're expecting a destructor call corresponding to the assignment aa[0] = a;. But assignments does not involve creation of new objects. To be more precise, you're doing copy assignment and not copy initialization.

In particular, your assignment statement as[0] = a; is the same as:

aa[0].operator=(a); //this involves no construction of any new objects

In the above statement, there is no creation of any new objects. So, there are only three A objects for which you get the 3 constructor and 3 destructor calls.

Jason
  • 36,170
  • 5
  • 26
  • 60