tohaz
tohaz

Reputation: 197

Default nested structure initialization

Is there a syntax to initialize DataItem dh.size element to sizeof(DataItem)? The code below uses constructor and relying on compiler to optimize things to constant values. Not the perfect solution... Is there a syntax to initialize nested structs?

struct DataHeader_t {
    int sz = 0;
    int type = 0;
};

struct DataItem {
    DataItem() {dh.sz = sizeof(DataItem);}
    DataHeader_t dh;
    float data1 = 0;
    float data2 = 0;
    ...
};

Upvotes: 0

Views: 381

Answers (1)

Marcus Müller
Marcus Müller

Reputation: 36442

Not the perfect solution... Is there a syntax to initialize nested structs?

Yes, that structure is called constructor, and you're already using it.

Upvotes: 2

Related Questions