Skip to content

Instantly share code, notes, and snippets.

@martinky
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinky/b13f1db0c750cdcf9365 to your computer and use it in GitHub Desktop.
Save martinky/b13f1db0c750cdcf9365 to your computer and use it in GitHub Desktop.
#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