Object with a Constructor as a member of a class

Is it possible to have a member in a class of a type of a class that has a constructor? As in:
1
2
3
4
5
6
7
8
9
class foo
{
    foo(int value);
};

class bar
{
    foo member(3);  //doesn't work
};

But that doesn't work; it throws "[Error] 'bar::member' does not have a class type". Probably the compiler is mistaking that for a function declaration.

Thanks in advance.
Yes it is possible, but the constructor should be specified in the constructor initialization list.

1
2
3
4
5
6
7
8
9
10
11
12
13
class foo
{
public:
	foo(int value);
};

class bar
{
public:
	bar() : member(3) {}
private:
	foo member;
};
Last edited on
Nice! I didn't know there was such a thing. But what if I want to initialize more than one constructor? Where can I read more about this?
Topic archived. No new replies allowed.