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.
我有两个结构它们90%的领域是相同的。我想将这些字段分组到一个结构中,但是我不想使用点操作符来访问它们。原因是我已经用第一个结构编写了代码,并且刚刚创建了第二个结构。
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:
将str1和atr2变成这样
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:
最后,我希望通过以下方式访问a、b和c:
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;
struct str11 {int a;int b;int c;};typedef struct str22: public str11 {QString name;}你好;
6 个解决方案
#1
10
Yes. Rather using a C-style of inheritance, using C++ style:
是的。使用C语言风格的继承,使用c++风格:
struct str3{
int a;
int b;
int c;
};
struct str2 : public str3{
object2 name;
};
#2
3
First one remark. This
第一个评论。这
struct
{
int a;
int b;
int c;
} str3;
defines an object str3, not a type str3.
定义对象str3,而不是类型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
2
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
1
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:
我不确定c++(我还在学习c++),但是在C语言中,有些编译器允许匿名结构。使用GCC 4.3,如果没有指定标志,但是没有使用-ansi或-std=c99进行编译,那么下面的编译没有错误。它成功的编译了-std=c+ 98,但是:
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
0
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).
不要使用愚蠢的技巧来避免适当的重构。它会帮你省下一点打字的时间,但也会让你的a.. .以后。如果编辑起来很复杂,那么你使用了错误的工具,或者你有一个糟糕的命名方案(1个字母的名字很难找到/替换)。
#6
#1
10
Yes. Rather using a C-style of inheritance, using C++ style:
是的。使用C语言风格的继承,使用c++风格:
struct str3{
int a;
int b;
int c;
};
struct str2 : public str3{
object2 name;
};
#2
3
First one remark. This
第一个评论。这
struct
{
int a;
int b;
int c;
} str3;
defines an object str3, not a type str3.
定义对象str3,而不是类型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
2
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
1
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:
我不确定c++(我还在学习c++),但是在C语言中,有些编译器允许匿名结构。使用GCC 4.3,如果没有指定标志,但是没有使用-ansi或-std=c99进行编译,那么下面的编译没有错误。它成功的编译了-std=c+ 98,但是:
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
0
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).
不要使用愚蠢的技巧来避免适当的重构。它会帮你省下一点打字的时间,但也会让你的a.. .以后。如果编辑起来很复杂,那么你使用了错误的工具,或者你有一个糟糕的命名方案(1个字母的名字很难找到/替换)。