Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

aggregate initialization in constructor...

10 views
Skip to first unread message

Chris M. Thomasson

unread,
Jan 12, 2017, 10:50:46 PM1/12/17
to
This is about using aggregate initialization in the constructor of a
struct/class that has an array. Basically, I am wondering if the
following code wrt the constructor of foo is kosher:
______________________________________________
#include <cstdio>


struct foo
{
struct inner
{
unsigned int a;
unsigned int b;
};

inner inner[3];
unsigned int a;
unsigned int b;

foo(
unsigned int a_inner,
unsigned int b_inner
): inner {
{ a_inner, b_inner },
{ a_inner + 1, b_inner + 2 },
{ a_inner + 3, b_inner + 4}
},
a(a_inner + b_inner),
b((a_inner + b_inner) / 2)
{

}
};


int main()
{
{
foo f(3, 5);

std::printf("f.inner[0].a:(%u)\nf.inner[0].b:(%u)\n",
f.inner[0].a, f.inner[0].b);


std::printf("f.inner[1].a:(%u)\nf.inner[1].b:(%u)\n",
f.inner[1].a, f.inner[1].b);


std::printf("f.inner[2].a:(%u)\nf.inner[2].b:(%u)\n",
f.inner[2].a, f.inner[2].b);

std::printf("f.a:(%u)\nf.b:(%u)\n", f.a, f.b);
}

return 0;
}
______________________________________________

Seems to work fine for me.
0 new messages