Is it possible to simplify this definition for object?

Is it possible to define this object without need to create types for each property? Something like: { group : { collapsed : 31, expanded : 120 } , terrain ... } (this is syntax from different language.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
typedef struct UWidthsbase_s {
	int collapsed;
    int expanded;
} UWidthsbase_t;

typedef struct UWidths_s {
	UWidthsbase_t group;
    UWidthsbase_t terrain;
    UWidthsbase_t const;
} UWidths_t;

UWidths_t UWidths;
UWidths.group.collapsed = 31;
UWidths.group.expanded = 100;
UWidths.terrain.collapsed = 31;
UWidths.terrain.expanded = 120;
UWidths.group.collapsed = 31;
UWidths.group.expanded = 140;
Sure you can:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct UWidthsbase_t 
{
    int collapsed;
    int expanded;
};

struct UWidths_t 
{
    UWidthsbase_t group;
    UWidthsbase_t terrain;
};

int main()
{
    UWidths_t UWidths { {31, 100}, {31, 120}};
}
If you want, you might add comments to avoid misunderstanding about init order:
UWidths_t UWidths { /*group =*/{31, 100}, /*terrain =*/{31, 120}};
Thanks.

Can you help me how to make pointer to the member?

1
2
UWidths_t UWidths { {31, 100}, {31, 120}, {31, 140}};
UWidthsbase_t * pUWidthsClass = UWidths.group;


Error 2 error C2065: 'UWidths' : undeclared identifier
left of '.group' must have class/struct/union
You need to store address in pointer:
UWidthsbase_t* pUWidthsClass = &UWidths.group;
Still I have Error 2 error C2065: 'UWidths' : undeclared identifier

1
2
3
4
UWidths_t UWidths { {31, 100}, {31, 120}, {31, 140}};
UWidthsbase_t * pUWidthsClass = &UWidths.group;
UWidthsbase_t * pUWidthsTerrain = &UWidths.terrain;
UWidthsbase_t * pUWidthsConst = &UWidths.uConst;
I am using Visual Studio 2010, is that the problem?
Strange. Everything should work. http://coliru.stacked-crooked.com/a/9e88efc762d4c83e
Is this a first error? Because previous errors might lead to false positives in later lines. Post minimal compiling example which reproduces your problem.
Last edited on
Some new error appeared:
error C2470: 'UWidths' : looks like a function definition, but there is no parameter list; skipping apparent body

Should not that be like so?

UWidths_t UWidths = { {31, 100}, {31, 120}, {31, 140}};
Last edited on
I am using Visual Studio 2010, is that the problem?
Yes, this is the problem. It does not support C++11 and uniform initialization. Add = after variable name: UWidths_t UWidths = { {31, ...
I did and works. THX
Topic archived. No new replies allowed.