Originally Posted by
Eman
So, when I pass by value
this happens
Not quite. Allow me to digress. A rule of thumb is that when you want to pass an object of class type to a function but not modify it, you pass it by const reference, e.g.,
Code:
void foo(const Dog& dog)
{
// ...
}
This avoids unnecessary copying. Sometimes, you really do want to make a copy to work on, so you write:
Code:
void foo(const Dog& dog)
{
Dog temp(dog);
// Now we can make changes to temp.
// ...
}
But you could also have written:
Code:
void foo(Dog dog)
{
// Now we can make changes to dog.
// ...
}
So, what happens is closer to defining the copy constructor like this:
Code:
Dog(const Dog& a)
{
Dog temp(a);
}