我可以访问struct里面的struct而不使用点运算符? [英] can I access a struct inside of a struct without using the dot operator?

查看:124
本文介绍了我可以访问struct里面的struct而不使用点运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个结构,其90%的字段相同。我想在一个结构中分组这些字段,但我不想使用点运算符来访问它们。原因是我已经用第一个结构编码,并且刚刚创建了第二个结构。

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.

之前:

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;

并将str1和atr2更改为:

and would change the str1 and atr2 to this:

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

typedef struct {
  str3 str;
  object2 name;
} str2;

最后,我希望能够通过执行以下操作来访问a,b和c:

Finally I would like to be able to access a,b and c by doing:

str1 myStruct;
myStruct.a;
myStruct.b;
myStruct.c;

而不是:

myStruct.str.a;
myStruct.str.b;
myStruct.str.c;

有办法做这样的事情。这样做的原因是我想保持数据的完整性,如果结构的chnges发生,并不重复自己,不必改变我现有的代码,并且没有嵌套太深的字段。

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.

已解决:thx为您的所有答案。最后一种方式,使我可以使用自动完成也是以下:

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;

推荐答案

是的。而是使用C风格的继承,使用C ++风格:

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

这篇关于我可以访问struct里面的struct而不使用点运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆