我可以访问一个结构struct的内部,而不使用点运算符? [英] can I access a struct inside of a struct without using the dot operator?

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

问题描述

我有2个结构有90%的领域一样。我想组中的结构,这些领域的,但我不想使用点运算符来访问它们。原因是我已经codeD与第一结构和刚才创建的第二个。

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;

最后,我想能够通过做访问,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;

有没有办法做这样的事情。这样做的原因,这是我要保持数据的integrety如果chnges到结构都发生并且不重复自己,而不必改变现有的code,而不必领域嵌套太深。

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:


  结构str11结果
  {结果
    int类型的;结果
    INT B:结果
    INT℃;结果
  };结果
  typedef结构str22:公共str11结果
  {结果
    名QString的;结果
  }喜;结果

推荐答案

是的。而使用继承的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的内部,而不使用点运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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