Structure initialization

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

Recommended Posts

Hi there! Consider this two structures: struct s_1{ float x,y; }; struct s_2:s_1{ float z; }; One can initialize an instance of type s_2 this way: s_2 s_2_instance={1.0f}; // here only z will be initialized Is there any way to initialize fields x and y of s_2_instance at design time? Actually i rarely need this but as i'm very performance minded programmer (several years programming in assembly) i'd like to know the way for such initialization. Thanks in advance.

Share on other sites
Use a constructor?

Share on other sites
Quote:
 Original post by Jay IIs there any way to initialize fields x and y of s_2_instance at design time? Actually i rarely need this but as i'm very performance minded programmer (several years programming in assembly) i'd like to know the way for such initialization. Thanks in advance.

If you were performance-minded, you'd write both examples in a real application, then examine the generated assembly. Or better yet, profile the real application. Right now, you just have an obsolete notion of performance.

This isn't meant in a derogatory way, it's just a common trait for assembly programmers to worry about a grain of sand while the sea is eroding the beach.

How many structures will you initialize, how often? 1? Per application life-time? Or 20 million on every loop, 60 loops per second? What kind of algorithm? Heap or stack allocates?

struct s_1 {  s_1(float x_ = 0, float y_ = 0)    : x(x_)    : y(y_)  {}  float x;  float y;};struct s_2 : public struct s_1 {  s_2(float x_ = 0, float y_ = 0, float z_ = 0)    : s_1(x,y)    , z(z_)  {}  float z;};int main() {  s_2 s_2_instance(1f,2f,3f);  std::cout << s_2.x << s_2.y << s_2.z << std::endl;}

If you examine the generated assembly, you may find that the result will be equivalent to:
int main() {  std::cout << 1.0f << 2.0f << 3.0f << '\n';}

Share on other sites
Hey, take it easy and be more attentive! Note that i used the phrase "design time" that means that memory is allocated when the program is being compiled. No malloc's or other runtime calls. Actually all what you guys posted was about runtime memory allocation. The only way i know to allocate (or reserve, if allocate is confusing for you) memory at design time in C/C++ is to define a global variable. When you use constructors it requires some code to be executed at runtime no matter how small constructors are. In assembly as in C/C++ 'design-time' memory is initialized using initializers. So my question may be transformed to this: is there any way to use initializers to initialize fields that are inherited and not explicitly defined in some structure type. I hope now you see that it's dumb to use a profiler or debugger to find an answer for a question about C++ syntax. ...For assembly programmers it's common to worry about every water drop caz it makes sence when your ocean becomes a pool.

Share on other sites
A programmer who has spent much of his time programming in assembly may come to assume that a compiler is a stupid beast who performs by rote and is incapable of making clever decisions. This is an apt description of an assembler, but not of modern compilers. Think of the compiler as a pretty good assembler programmer, who might not have the creativity to refactor a large-scale algorithm, but who at the very least doesn't crank out manifestly dumb code.

Share on other sites
Quote:
 Original post by Jay IHey, take it easy and be more attentive! Note that i used the phrase "design time" that means that memory is allocated when the program is being compiled.
The phrase you're looking for is "compile time".

Now to answer your original question. Brace-enclosed initialiser lists can only be used to initialise aggregates and scalar types. Aggregates cannot have base classes. So no, there is no way to initialise the members of a base class using brace-enclosed initialiser lists.

Σnigma

Share on other sites
Thanks Enigma. Short and simple answer. What about my phrase 'design time' it is used interchangeably with 'compile time' in lots of docs and specs and means absolutely the same.

Share on other sites
Design-time is the UML charts and non-code thing, pencil and paper, Visio.
Compile-time is the pre-processor/template instantiation/assembly generation/compile-time polymorphism.
Run-time is dynamic polymorphism and stuff that is evaluated when code is running.

Share on other sites
The reason nobody answered your question is because they were trying to help you with the actual problem that led you to ask your question. IMO helping you get a better idea of how C++ works and giving you tips to use it effectively is infinitely more valuable than saying "no you can't do that" and sending you on your way.

Share on other sites
And with C++09 incoming: C++09 will have brace initializer lists options for non-aggregates.

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• Forum Statistics

• Total Topics
632960
• Total Posts
3009479
• Who's Online (See full list)

There are no registered users currently online

×