samedi 30 juillet 2016

Weird constructors behavior

#include <iostream>
using namespace std;

class Foo
{
public:
    Foo()
    {
        cout << 0;
    }
    Foo(Foo &f)
    {
        cout << 1;
    }
};

void someFunction(Foo f) {};

int main()
{
    Foo f1;           //displays 0 (as expected)
    Foo f2(f1);       //displays 1 (as expected)
    someFunction(f1); //displays 1 (why?)
    someFunction(f2); //displays 1 (why?)

    return 0;
}

I don't understand why function 'someFunction' calls second constructor. I thought it will just call first constructor, with no parameters, and displays 0.

Maybe I am missing something obvious...

Aucun commentaire:

Enregistrer un commentaire