Quote Originally Posted by laserlight View Post
It is by virtue of the parameter being of type Dog. Try this program:
Code:
#include <iostream>
#include <cstdlib>

class Dog;

void foo(Dog dog);

class Dog
{
public:
    Dog() {}

    Dog(const Dog& other)
    {
        std::cout << "Dog copy ctor" << std::endl;
        foo(other);
    }
};

void foo(Dog dog)
{
    std::cout << "foo" << std::endl;
    std::exit(0);
}

int main()
{
    Dog main_dog;
    foo(main_dog);
}
Observe that foo exits from the program yet there is the infinite loop. You can conclude that the std::exit(0); and in fact the whole body of the foo function, is unreachable. It is unreachable because its parameter is of type Dog, causing the copy constructor to be invoked. But the copy constructor prints a message then invokes foo, which in turn causes the copy constructor to be invoked...

So, if what you were trying to do were legal, we would be bypassing this additional foo function and just combining it into a single constructor. The effects would be the same.
Wow yeah your code makes sense.

so
foo(main_dog) //main_dog invokes the copy constructor, which after executing the cout statements, calls the function again, thereby a recursive call.
Although in this case you are inducing the recursion.

Are you saying this is what happens ?

When i did

Dog a( b) // b is an existing object

are you saying that the instance "Dog a(b)" is always invoked by the constructor?

Why is that happening?
I mean in your code it is clear why it is happening, but in the copy constructor we wrote.
We passed by value, yes, we do not however call the instance again...