I have a structure with unnamed structure inside. I want to initialize whole structure and it's member structure in class initializer list.
struct Foo {
int z;
struct {
double upper;
double lower;
} x, y;
};
class Bar {
Bar();
Foo foo;
};
Can this be done?
Also can this structure be initialized "old fashion" way providing a constructor without uniform initialization syntax?
struct Foo {
Foo() : z(2), x(/*?*/), y(/*?*/) {}
Foo() : z(2), x.lower(2) {} // doesn't compile
int z;
struct {
double upper;
double lower;
} x, y;
};
If I understand you correctly you want to initialize the struct Foo
, which contains an unnamed struct, in the initializer list of Bar
:
#include <iostream>
struct Foo {
int z;
struct {
double upper;
double lower;
} x, y;
};
class Bar {
public:
Bar();
Foo foo;
};
Bar::Bar()
: foo { 1, { 2.2, 3.3}, {4.4, 5.5} }
{
}
int main()
{
Bar b;
std::cout << b.foo.z << std::endl;
std::cout << b.foo.x.upper << std::endl;
std::cout << b.foo.y.lower << std::endl;
}