Is this possible?
I know that you can initialize structs using list syntax.
IE
struct Foo f = { a, b, c};
return f;
is a possible to do this in one line as you would be with classes and constructors?
Thanks
Is this possible?
I know that you can initialize structs using list syntax.
IE
struct Foo f = { a, b, c};
return f;
is a possible to do this in one line as you would be with classes and constructors?
Thanks
Create a constructor for the struct (just like a class) and just do
return Foo(a,b,c);
Edit: just to clarify: structs in C++ are just like classes with the minor difference that their default access-permission is public (and not private like in a class). Therefore you can create a constructor very simply, like:
struct Foo {
int a;
Foo(int value) : a(value) {}
};
If you want your struct to remain a POD, use a function that creates it:
Foo make_foo(int a, int b, int c) {
Foo f = { a, b, c };
return f;
}
Foo test() {
return make_foo(1, 2, 3);
}
With C++0x uniform initialization removes the need for that function:
Foo test() {
return Foo{1, 2, 3};
// or just:
return {1, 2, 3};
}