8

Does the class destructor get called when a variable that already holds an object receives another object in a c++ code?

Car car1;
Car car2;

car1 = car2;

Does the car1 destructor get called in this situation?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
RenanJosé
  • 83
  • 1
  • 4

5 Answers5

5

The destructor of car1 will not be executed when you do

car1 = car2;

Only the (probably implicitly generated) Car::operator= (const Car&); will be called on car1.

The destructor will only be called when car1 goes out of scope (or when you call it explicitly, but you really rarely need that).

Also note that car1 does not "hold" a Car instance, it is the instance itself.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
3

You can see in the following program that the destructor does not get called for t1 or t2 till the end of main() function:

#include <iostream>
#include <string>

class Test
{
    std::string _name;
public:
    Test(std::string name) : _name(name) { }
    ~Test()
    {
        std::cout << "Destructor " << _name << std::endl;
    }
    Test& operator=(const Test& fellow)
    {
        // avoid changing the name of the object
        std::cout << "Assignment operator " 
            << _name << "=" << fellow._name << std::endl;
        return *this;
    }
};

int main()
{
    Test t1("t1"), t2("t2");
    t1 = t2;
    return 0;
}

In an assignment t1=t2, just the assignment operator is called on t1 taking t2 as a parameter. If you need to release the resources of t1, you can do that in the assignment operator implemented as the code example shows. Don't forget to implement copy constructor too - that's for the cases of assignment to uninitialized instance (no need to release previously held resources because there are no resources held by the time of copy constructor call).

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • @BenjaminLindley, move assignment operator would steal resources from t2, so that there would be nothing to release for destructor of t2. And move assignment operator is for cases like returning a temporary value from a function: t1 = f(). While copy assignment operator is for cases like the one discussed in this thread: t1=t2. What's wrong with releasing resources of t1 in copy assignment operator? – Serge Rogatch Jun 28 '15 at 17:28
  • My mistake. I was misreading that as releasing the resources of the right hand side operand. – Benjamin Lindley Jun 28 '15 at 17:31
  • How release the resources of t1 in the assignment operator if the object is const? – RenanJosé Jun 28 '15 at 17:44
  • @RenanJosé You cannot assign to `const` objects, so this question makes no sense. – Baum mit Augen Jun 28 '15 at 17:45
  • A doubt. What is the assignment operator calling on t1 = t2;? t1 or t2? – RenanJosé Jun 28 '15 at 17:50
  • @RenanJosé, for t1=t2 assignment operator having "t1" as "this" and "t2" as an argument is called. If t1 is const, you can still run t1=t2 if you declare an assignment operator as const and release resources which you declare as mutable. But this seems a bad style. – Serge Rogatch Jun 28 '15 at 17:56
1
Car car1();//error, Car car1; call default construct function
Car car2(); //error, Car car2;call default construct function

car1 = car2; //call operator=() 
kiviak
  • 1,083
  • 9
  • 10
0

Well it depends. If you allocate memory on the heap and assign one variable to another it wont call the destructor:

{
    Car* car1 = new Car();
    Car* car2 = new Car();
    car1 = car2;
}

But this will, and its because it goes out of scope not because of the copy assignment.

{
    Car car1;
    Car car2;
    car1 = car2;
}
  • What's so confusing. It pretty much shows you that the operator=() doesn't deallocate memory by default and the only reason the destructor gets called in example two is due to the scope and not the assignment itself. –  Jun 28 '15 at 17:20
0
car1 = car2;

this causes the destructer of car1 NOT to be executed.
if you want the destructor to be called, car1 needs to be called Explicitly or it should go out of scope.(as our Friend Baum mit Augen said, calling car1 Explicitly is rarely in need).

marc_s
  • 455
  • 1
  • 4
  • 15