1

Sorry i am struggling to grasp copy constructors,i would like to know why does the copy constructor get invoked only when i call a object in a function by value "op.return_Value(op)<

class operation{
   public:
       int add(int x,int y){
           *total=x+y;
       return (*total);
       }

       operation(){
       cout<<"This is the constructor"<<endl;
       total=new int;
       }
       operation(const operation &op){
       cout<<"This is the copy of the start"<<endl;
       total=new int;
       *total=*op.total;
       }

       ~operation(){
       cout<<"This is the end";
       delete total;
       }

        int *total;

       int return_Value(operation op){
        return *total;
       }

};

class child_operation:operation{

   public:

       int sub(int x,int y){
           *total=x-y;
       return(*total);
       }

};



int main()
{
   operation op;
   child_operation op1;

   cout<<op.add(5,6)<<endl<<op1.sub(6,5)<<endl;
   cout<<op.return_Value(op)<<endl;
}

Basically in which ways is a copy constructor invoked?

Zibele
  • 15
  • 1
  • 5
  • 1
    I can recommend getting a book which covers the basics of C++ programming to handle these sorts of questions. –  Aug 19 '15 at 12:00

2 Answers2

3

It is pretty straightforward, and explicitly stated in the standard (12.8.1):

A class object can be copied in two ways, by initialization (including for function argument passing and for function value return) and by assignment. Conceptually, these two operations are implemented by a copy constructor and copy assignment operator.

So, if you are initializing a new object, either explicitly, or implicitly (e.g. passing an object into a function by copy, which is your case, or returning an object from a function), a copy constructor is called:

MyClass a = b;//initialization, copy constructor is called
//or
void foo(MyClass a){}//a is passed by value, a copy will be made, and copy constructor will be called
//or
MyClass foo()
{
    MyClass result;
    return result;//a copy of result will be returned, and copy constructor will be called
}

On the contrary, if you are making an assignment, the operator= will be called instead, e.g.

MyClass x;
MyClass y;
y = x;//assignmanet
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • In your last example, returning a function-local variable (i.e. `result`) the copy will surely be [elided](https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization) as an optimization. (Though without optimizations, you are correct, that would invoke a copy) – Cory Kramer Aug 19 '15 at 12:08
  • Actually, function return is defined in terms of move, not copy (with copy as a fallback). – MikeMB Aug 19 '15 at 12:12
  • @CoryKramer, I know, but I decided not to mention it because IMO it's not really relevant to the question. – SingerOfTheFall Aug 19 '15 at 12:12
2

In this function

int return_Value(operation op){
    return *total;
}

You are passing your operation by value. Therefore op is a function local variable that is a copy of the operation you passed in. To avoid this copy, you could pass the operation as a const&

int return_Value(operation const& op){
    return *total;
}

On a side note, I don't think this function needs to take any arguments. If it is just supposed to be a getter method, it will operate off of this, so you shouldn't pass any argument in, and the method should be const

int return_Value() const {
    return *total;
}

I also don't know why you are storing total as an int* instead of an int.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218