Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
class Component
{
public:
int value;
Component(int v) :
value(v)
{
printf("Component(%d)\n", value);
}
};
class Composite
{
public:
Component a;
Component b;
/*
Although the initializer list is written in the order: b, a, the member
constructors will be executed in declaration order: a, b.
*/
Composite() :
b(1),
a(b.value + 1) // Counterintuitive: b.value is undefined at this line.
{ }
};
int main(int, char**)
{
/*
Here, one would expect the following output:
> Component(1)
> Component(2)
But instead we will get:
> Component(<random int value>)
> Component(1)
*/
Composite comp;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment