Why is the copy constructor called when we pass an object as an argument by value to a method?
Why is the copy constructor called when I pass an object as an argument by value to a function? Please see my below code: I am passing an object of a class as an argument by value to a function display()
, but it is calling the copy constructor before control is hitting the display()
function. I don't understand why.
#include "stdafx.h"
#include <iostream>
using namespace std;
class ClassA
{
private:
int a, b;
public:
ClassA()
{
a = 10, b = 20;
}
ClassA(ClassA &obj)
{
cout << "copy constructor called" << endl;
a = obj.a;
b = obj.b;
}
};
void display(ClassA obj)
{
cout << "Hello World" << endl;
}
int main()
{
ClassA obj;
display(obj);
return 0;
}
To elaborate the two answers already given a bit:
When you define variables to be "the same as" some other variable, you have basically two possibilities:
ClassA aCopy = someOtherA; //copy
ClassA& aRef = someOtherA; //reference
Instead of non-const lvalue references, there are of course const references and rvalue references. The main thing I want to point out here is, that aCopy
is independent of someOtherA
, while aRef
is practically the same variable as someOtherA
, it's just another name (alias) for it.
With function parameters, it's basically the same. When the parameter is a reference, it gets bound to the argument when the function is called, and it's just an alias for that argument. That means, what you do with the parameter, you do with the argument:
void f(int& iRef) {
++iRef;
}
int main() {
int i = 5;
f(i); //i becomes 6, because iRef IS i
}
When the parameter is a value, it is only a copy of the argument, so regardless of what you do to the parameter, the argument remains unchanged.
void f(int iCopy) {
++iCopy;
}
int main() {
int i = 5;
f(i); //i remains 5, because iCopy IS NOT i
}
When you pass by value, the parameter is a new object. It has to be, since it's not the same as the argument, it's independent. Creating a new object that is a copy of the argument, means calling the copy constructor or move constructor, depending on whether the argument is an lvalue or rvalue. In your case, making the function pass by value is unnecessary, because you only read the argument.
There is a Guideline from GotW #4:
Prefer passing a read-only parameter by const& if you are only going to read from it (not make a copy of it).
Because passing by value to a function means the function has its own copy of the object. To this end, the copy constructor is called.
void display(ClassA obj)
{
// display has its own ClassA object, a copy of the input
cout << "Hello World" << endl;
}
Bear in mind that in some cases copies may be elided, for instance, if a temporary value is passed to the function.
As juanchopanza said:, you pass by value, which causes a copy to be made. If you want to prevent this you can pass by reference:
void display(const ClassA &obj)
On a sidenote: You should declare your copy ctor to take the argument as const reference:
ClassA(const ClassA &obj)
Otherwise you won't be able to use the copy ctor on named objects marked as const or with temporaries. It also prevents you from accidentally changing the passed in object.