0
class Entity
{
  public:
    int a;
    Entity(int t)
      :a(t)
    {
        std::cout << "Constructor !" << std::endl;
    }
    ~Entity()
    {
        std::cout << "Destructor !" << std::endl;
    }

    Entity(Entity& o)
    {
        std::cout << "Copied !" << std::endl;
        this->a = o.a;
    }
};

Entity hi()
{
    Entity oi(3);
   return oi;
} 

int main()
{
  {
        Entity o(1);
        o = hi();
  }
     std::cin.get();
}

OUTPUT:

Constructor !

Constructor !

Copied !

Destructor !

Destructor !

Destructor !


I created two objects and I copied one, So three constructors and three destructors.

Bubesh p
  • 65
  • 1
  • 8

2 Answers2

2

Your "Copied!" line in the output is coming from the copy constructor, so you're creating three objects, not just two (then you're destroying all three, as expected).

Note that a copy constructor should normally take its argument by const reference. At a guess, you may be using Microsoft's C++ compiler, which will bind a temporary to a non-const reference.

Also note that if you turn on optimization, you can probably expect to see just two constructors and two destructors, with no copy construction happening. With a new enough (C++17) compiler, that should happen even if you don't turn on optimization (copy elision has become mandatory).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • so copy constructor creates temporary object(with values from object in hi()) with main function scope. and copies the values from temporary object into object(o)? – Bubesh p Mar 10 '19 at 18:04
  • @Bubeshp: Yes, `hi()` creates and returns an object, and `o` is copy constructed from the object it returns. – Jerry Coffin Mar 11 '19 at 00:02
1

This is a trivial question

Can any one explain the reason for three destructor?

when you call

o=hi();

your function is called which makes an object of type Entity , which in return calls the constructor. This is where you get one extra constructor message

Replace your Entity(int t) contructor by this

 Entity(int t)
   :a(t)
  {
    std::cout << "Constructor created with integer "<< a << std::endl;
  }

You will see which constructors were called when you run the code.

  • so copy constructor creates temporary object(with values from object in hi()) with main function scope. and copies the values from temporary object into object(o)? – Bubesh p Mar 10 '19 at 18:05
  • nope @Bubeshp . remember the format for copy constructor of a class A is A(A&) this means you are passing by reference . so no copying here. Think of it as you have an object of class A say obj1 which has some values n. now later you want to assign this value to newly created obj (say obj2). What you can do is A obj2 = obj1. this will call the method A(A& obj1) and whatever code inside the copy contructor you have written will be executed. so if you have no code that assigns value from obj1 to obj2 then you will not have copied it. Remeber it just calls the copy constructor – Roushan Singh Mar 10 '19 at 18:31
  • @Bubeshp Observe Entity oi returned from hi; so o=hi(); becomes o=oi; and going by previous comment copy constructor is called. – Roushan Singh Mar 10 '19 at 18:33
  • so only two objects? – Bubesh p Mar 10 '19 at 18:34
  • yeah only two objects are created. but you have called contructors 2 times while making objects o(1) , oi(3) and copy constructor once when you did o = hi(); – Roushan Singh Mar 10 '19 at 18:37
  • so what will happen if I freed some variable inside destructor like delete a; .here destructor is called 3 times, so it will deallocate the 3 memory right? – Bubesh p Mar 10 '19 at 18:41
  • 1st delete for oi(3) – Bubesh p Mar 10 '19 at 18:42
  • 2nd delete for o(1) then what about another delete(from 3rd destructor)? – Bubesh p Mar 10 '19 at 18:42
  • can you please tell me compiler its version and operating system. Cause on linux i am getting 2 under g++ – Roushan Singh Mar 10 '19 at 18:45
  • windows microsoft visual studio c++17 – Bubesh p Mar 10 '19 at 18:46
  • And Thank you for spending time for me.:) – Bubesh p Mar 10 '19 at 18:46
  • I think it has to do with settings in visual studio. https://www.onlinegdb.com/online_c++_compiler. i used this compiler and click on the top left and select c++17 it shows only 2 contructors and 2 destructors. Its normal different compilers behave differently on different os's as well – Roushan Singh Mar 10 '19 at 18:53
  • in debug mode(without optimization) I am getting 3 constructors. In release mode(with optimization) I am getting 2 – Bubesh p Mar 10 '19 at 18:56