Error passing temp object

Hi guys. Given the simple class definitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Bar
{
public:
    Bar(): value(21) {}
    Bar(int i): value(i) {}
private
    int value;
};

class Foo
{
public:
    Foo(Bar b) 
    { 
        cout << "constructor called" << endl; 
    }
};

// for this the message "constructor called" is printed on the screen
Foo f(Bar(13));

// but not for these
Foo f(Bar()); 
Foo f(Bar);

I think the compiler sees the last two
as function declarations or something

So how can I create a Foo object with the Bar default constructor ?
I know this works:
1
2
3
Foo(Bar b = Bar()) 
//and then
Foo f;

but suppose I couldn't modify Foo.

Thanks in advance.
Foo f = Foo( Bar() );
Thanks.

So there is no way to do it the way I was trying before?
I mean
 
Foo f(Bar(13));

is ok but

1
2
Foo f(Bar()); 
Foo f(Bar);

are errors ?
Last edited on
Thanks jsmith, very nice faq.
Topic archived. No new replies allowed.