views:

47

answers:

2

How can I initilise a structure in the constructor list?

Say:

struct St{int x, y};

class Foo
{
public:
Foo(int a = 0, int b = 0) : /*here initilise st_foo out of a and b*/
{}

private:
const St st_foo;
};
+3  A: 

You need to provide a constructor for struct St that takes 2 int parameters and assigns them to x and y. Then invoke it in your Foo initialization list.

Remember: structs are the same thing as classes except default visibility rules (for structs, default is public.

pajton
Thanks, I got it now.The code works now.
m4design
A: 

Here is an answer in case you want zero initialization for the struct (a solution for c++0x is also pointed out for the general case). It is sufficient to put an empty pair of parentheses after the member's name:

Foo() : st_foo()
rafak