1

Suppose, we have defined class Def with constructor Def(int).

If we have following code:

{
   Def obj; //calls default constructor
   Def obj1(3); //calls Def(int) constructor

   obj=Def(3);
   /*calls Def(int) constructor to create temporary on the right side,
     calls assignment   operator (provided by C++) and finally calls
     destructor to destroy temporary object*/

   Def obj2=Def(7);
   /*I thought that here it would create temporary on the right side,
     than call copy constructor and call destructor to destroy temporary object,
     but this doesn't happen. Destructor is not called after expression is
     finished in this scenario.*/    
}

Could someone explain me why destructor is not called in the last expression? Thanks!

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
user3616359
  • 359
  • 3
  • 20

0 Answers0