can I access a struct inside of a struct without u

2019-05-14 04:38发布

问题:

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:

typedef struct{
  int a;
  int b;
  int c;
  object1 name;
} str1;  

typedef struct{
  int a;
  int b;
  int c;
  object2 name;
} str2;

now I would create a third struct:

typedef struct{
  int a;
  int b;
  int c;
} str3;

and would change the str1 and atr2 to this:

typedef struct{
  str3 str;
  object1 name;
} str1;

typedef 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;

回答1:

Yes. Rather using a C-style of inheritance, using C++ style:

struct str3{
  int a;
  int b;
  int c;
};

struct str2 : public str3{
  object2 name;
};


回答2:

First one remark. This

struct
{
  int a;
  int b;
  int c;
} str3;

defines an object str3, not a type str3.

You can achieve what You want using inheritance, but I suggest changing the numbers of Your structs

struct str1
{
  int a;
  int b;
  int c;
};


struct str2 : public str1
{
  object1 name;
};  


回答3:

The reason for doing this is I want keep the integrety of the data if chnges to the struct were to occur

To achieve that i'd rather use proper encapsulation, accessors and inheritance to make changes in layout invisible from user code:

class DataHolder {
    int a_, b_, c_;
public:
    int a() const { return a_; }
    int b() const { return b_; }
    int c() const { return c_; }
};

class User : public DataHolder {
    object o_;
public:
    object& getObject() { return o_; }
};


回答4:

I'm not sure about C++ (I'm still learning C++) but in C, some compilers allow anonymous structs. With GCC 4.3, the following compiles with no errors when no flags are specified, but fails to compile with -ansi or -std=c99. It compiles successfully with -std=c++98 however:


int main (int argc, char *argv[])
{
    struct
    {
        struct
        {
            int a;
            int b;
            int c;
        };
        int test;
    } global;

    global.a = 1;
    global.b = 2;
    global.c = 3;
    global.test = 4;

    return global.b;
}


回答5:

Don't use silly tricks to avoid proper refactoring. It will save you a little bit of typing but it will bite in your a.. later. If it is that complicated to edit, then you're a using the wrong tools and/or you have a bad naming scheme (1 letter names are difficult to find/replace).



回答6:

You can do it with GCC, as other poster mentioned, but in addition to this you can use -fms-extensions flag that allows you to use earlier defined struct as unnamed field.

More here.



标签: c++ c struct