can I access a struct inside of a struct without using the dot operator?
Posted
by yan bellavance
on Stack Overflow
See other posts from Stack Overflow
or by yan bellavance
Published on 2010-05-10T23:31:45Z
Indexed on
2010/05/11
0:14 UTC
Read the original article
Hit count: 575
I have 2 structures that have 90% of their fields the same. I want to group those fields in a structure but I do not want to use the dot operator to access them. The reason is I already coded with the first structure and have just created the second one.
before:
struct{
int a;
int b;
int c;
object1 name;
} str1;
struct{
int a;
int b;
int c;
object2 name;
} str2;
now I would create a third struct:
struct{
int a;
int b;
int c;
} str3;
and would change the str1 and atr2 to this:
struct{
str3 str;
object1 name;
} str1;
struct {
str3 str;
object2 name;
} str2;
Finally I would like to be able to access a,b and c by doing:
str1 myStruct;
myStruct.a;
myStruct.b;
myStruct.c;
and not:
myStruct.str.a;
myStruct.str.b;
myStruct.str.c;
Is there a way to do such a thing. The reason for doing this is I want keep the integrety of the data if chnges to the struct were to occur and to not repeat myself and not have to change my existing code and not have fields nested too deeply.
RESOLVED: thx for all your answers. The final way of doing it so that I could use auto-completion also was the following:
struct str11
{
int a;
int b;
int c;
};
typedef struct str22 : public str11
{
QString name;
}hi;
© Stack Overflow or respective owner