Last active
August 29, 2015 14:02
-
-
Save martinky/b13f1db0c750cdcf9365 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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