Structure initialization

Started by
10 comments, last by SiCrane 16 years ago
The only thing i needed to know was whether one can initialize inherited fields with initializers and no more so all your assumptions about what led me to the question are automatically wrong. However thanks for your help. I got the answer. Thanks.
ubuntu jaunty amd64
Advertisement
Quote:Original post by Jay I
When you use constructors it requires some code to be executed at runtime no matter how small constructors are.

Incorrect.

Here's Antheus' code corrected for compiler errors containing a global initialized with a constructor.
struct s_1 {  s_1(float x_ = 0, float y_ = 0)    : x(x_)    , y(y_)  {}  float x;  float y;};struct s_2 : s_1 {  s_2(float x_ = 0, float y_ = 0, float z_ = 0)    : s_1(x_, y_)    , z(z_)  {}  float z;}; s_2 s_2_instance(1.f, 2.f, 3.f);


Here's the assembly generated for s_2_instance by MSVC 2003 in release mode for s_2_instance:
PUBLIC	?s_2_instance@@3Us_2@@A				; s_2_instance_DATA	SEGMENT?s_2_instance@@3Us_2@@A DD 03f800000r		; 1	; s_2_instance	DD	040000000r			; 2	DD	040400000r			; 3_DATA	ENDSEND

Note the complete lack of anything done at runtime. All the values are, in fact, initialized at compile time.

This fact you could have easily determined for yourself had you actually listened to people rather than ignoring them when they didn't tell you exactly what you wanted to hear.

This topic is closed to new replies.

Advertisement