Understanding Structures

I understand the premise behind structures, being that they are a storage area for data but what I'm confused about is how the info is grouped inside of it. I have looked at the tutorial and am a bit confused. Would it be true that when a structure is populated, all info of similar types are grouped together? Ie...ALL "strings" get stored under the string type and ALL INT data get grouped as int?
It is not a storage it is a user defined type. Defining the struct tells the compiler the info. Not sure what you mean groups and populate , where have you heard that ?
closed account (48T7M4Gy)
The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is a region of storage.
Would it be true that when a structure is populated, all info of similar types are grouped together? Ie...ALL "strings" get stored under the string type and ALL INT data get grouped as int?
No, the order of the items is important regarding initialization.
Example when order is important:
1
2
3
4
5
6
7
8
struct foo
{
    std::string text;
    int size;
    std::string size_text;

    foo(std::string t) : text(std::move(t)), size(text.size()), size_text(std::to_string(size)) {} 
};
Here size sould be initialized after text, but before size_text. If we change order of members then we will break this dependency chain (for example if we will place size first, it will be initialized first and will try to request size of uninitialized string)
Last edited on
Structure are the very simple concept..It is a collection of different types of variables under a same common name.
There is a main difference between array and structures.Arrays: It is collection of all similar data types under a same common name.
thanks for the help. What I really meant was that a structure can serve as something like a record and after a little more digging, I discovered that what I want to use is an array of structures. I think. Just gotta polish it up a little.
look also for structure of arrays (aos vs soa).
Topic archived. No new replies allowed.