• Advertisement
Sign in to follow this  

Is that way of using constructor really better ?

This topic is 1935 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


IIRC, it's so that member variables would satisfy the last constructed, first destroyed order that is the general case for variables whose lifetime are managed by the compiler
Ah that makes sense -- if it went off the initializer list, and a class had 2 different constructors with different initialization orders, then the destruction order would no longer be the opposite (unless you error'd on mismatching initialization orders, or generated multiple virtual destructors).
That said, I'd still prefer the language to do that -- either generate an error if I've got multiple constructors with different orders, or simply warn me that my destruction order has become implementation defined due to the conflicting constructors.
In theory, the padding/member variable order issue was addressed by allowing the compiler to reorder position of member variables with different access specifiers, but in practice compilers don't actually take advantage of that.[/quote]Wait, the spec says the compiler is allowed to do that? Even after a decade of using this language I'm still learning new quirks...

Share this post


Link to post
Share on other sites
Advertisement
C++03 allowed it anyways. I don't have a copy of C++11 handy right now, but I seem to remember it walked back that flexibility somewhat. The verbiage for C++03 goes:

Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified.[/quote]
Since it says "intervening access-specifier" you could, in theory, stick a public: or whatever in front of each and every member variable and the compiler could reshuffle them to minimize padding.

Share this post


Link to post
Share on other sites

[quote name='taz0010' timestamp='1349502023' post='4987326']
Personally if I have primitive variables that I need to initialise in a specific order, I'm not going to rely on the class definition order to do it, and that means good old fashioned assignment with the = operator.

I emboldened the key part in the above quote -- primitive types aren't default initialized, so when it comes to assigning their default values, then the choice of using the initializer list, or assignment in the constructor body is purely a choice of style. They both generate the same results (unless of course, another item in the initializer list is initialized using the value of a primitive member), so saying one is more correct than the other is nothing more than a style war.[/quote]
Which is sortof the point I was poorly trying to make. Avoiding the initialization lists because it's 'not reliable' doesn't make sense to me, and being a choice of style, to call one less reliable when they are both standardized is misinformation. What you're saying though, is one is more resistant to side-effects of later code changes, and not that it's non-standard.

Personally if I have primitive variables that I need to initialise in a specific order, I'm not going to rely on the class definition order to do it, and that means good old fashioned assignment with the = operator.


I misunderstood taz's point - I thought he was implying that the class definition order is non-standardized, or unreliable. But you are saying that the humans during a crunch are unreliable, and easily mess up important initialization orders by mistake. That makes sense!

Offtopicly: Re-ordering the member variables by size seems rather silly to me (about as silly as re-ordering them based on alphabetizing their variable name), I've never worked in such an environment so there is probably have a good reason for it. Personally, in the few (having not worked on a compact platform) situations I needed to micro-manage the size of variables, I left comments mentioning each of their sizes. How does re-ordering by size help?

([size=2]I realize primitive types aren't initialized, and was mixing up initialization with construction - whoops!) Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
On most platforms types have alignment, a multiple of a memory address that it is most efficient to read or write that type. For primitives, alignment is generally equal to size. For structs and classes, alignment is generally equal to the alignment of the member variable with the most restrictive alignment requirements. On some platforms alignment is actually necessary to prevent a hardware exception; on other platforms it simply requires more clock cycles. For this reason C++ compilers will arrange structures so that every member variable is placed at proper alignment. Let's say you have the structure

struct A {
short aa;
int bb;
short cc;
};

With a two byte short and a four byte int, aa will generally be put at an offset of 0 into the struct, bb at an offset of 4 and cc at an offset of 8. Since A has an alignment of the most restrictive member, it will have an alignment of 4, and the whole structure will take up 12 bytes. However if you reorder the variables:

struct A {
short aa;
short cc;
int bb;
};

The aa will have an index of 0, cc an index of 2 and bb will have an index of 4, and the whole struct will take up only 8 bytes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement