copy constructor problame

Hi, I don't understand what is wrong with my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class foo
{
    public:

    foo(){};
    foo (foo& f) {};
    foo f(){ return *this; }

};


int main()
{
    foo a;

    foo b(a.f());

    return 0;
}


The compiler said, that :
no matching function for call to 'foo::foo(foo)'
teszt.cpp:10: note: candidates are: foo::foo(foo&)
teszt.cpp:9: note: foo::foo()

Why can't it make a reference to the return value?
When I write the copy construcot's parameter as a const, it works.
line six should be foo (const foo& f) {};
You can't bind temporaries to non-const references.

a.f() returns a temporary object.

foo's incorrect copy constructor takes a foo by non-const reference.

You are attempting to bind a.f() (temporary) to the non-const reference
parameter to the constructor.

a.f() returns a copy of a foo.
Thank for the answers, now it's clear for me. :)
Topic archived. No new replies allowed.