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
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.