I have a class in which i have defined:
- Constructor
- Destructor
- Copy constructor
- Assignment operator
operator +(Class& obj)
to add two objects.
operator+
returns by value as follows:
Base operator +(Base& obj2)
{
cout<<"+ operator called\n";
Base tmp;
tmp.x=x+obj2.x;
tmp.y=y+obj2.y;
return tmp;
}
If in my main function I create three objects. There should be three destructor calls:
Base obj1(1,2);
Base obj2(1,2);
Base obj3=obj1+obj2;
Questions:
In the body of
operator +()
, whilereturn tmp;
i see it calls my copy constructor. Is it that this tmp of type Base is getting stored in a temporary object generated by compiler internally (e.g. Base compiler_tmp=tmp) and hence the copy constructor is called?If yes, then i suppose it should represent the RHS of the expression
obj3=obj1+obj2;
and the destructor should be called at the end of this statement.
Are my above assumptions correct? Or I am missing a point here?