If I don't define a constructor in a struct, I can initialize it by just picking a certain value like this:
struct Foo {
int x, y;
};
Foo foo = {.y = 1};
But if I add new default constructor then I lose this feature:
struct Bar {
int x, y;
Bar(int value) : x(value), y(value) {}
};
Bar bar1 = 1;
Bar bar2 = {.y = 2}; // error: a designator cannot be used with a non-aggregate type "Bar"
Is it possible to have both ways?
I tried adding the default constructor Bar () {}
but it seems to not work either.
= default
instead of{}
– ChristeenchristelBar() = default;
– DorelleBar(Vec2 value) : x(value.x), y(value.y){};
but then I have to use double parens like thisBar bar2 = {{.y = 2}}
– Dorelle