我在代码中声明了2-3个结构,所有这些结构都有一些常见的数据类型。在我们公司,严格的政策是我们不重复代码。所以,我想知道,我是否可以在某些函数中声明这些公共数据类型,并在声明结构时使用该函数。
示例
struct_1
{
... un common stuff
// below are common declaration .. how would I declare below data type in some function and
// call it here to declare those data type
unsigned char char_1;
unsigned int int_1;
std::vector< small_structure> small_struct;
}
struct_2
{
... un common stuff
unsigned char char_1;
unsigned int int_1;
std::vector< small_structure> small_struct;
}
struct_3
{
... un common stuff
unsigned char char_1;
unsigned int int_1;
std::vector< small_structure> small_struct;
}发布于 2014-06-24 08:17:44
那么,为什么不创建一个共同的结构呢?
struct Common {
unsigned char char_1;
unsigned int int_1;
std::vector< small_structure> small_struct;
}
struct struct_3
{
... un common stuff
struct Common commonStuff;
}或者,如果使用C++,则可以从公共结构继承:
struct struct_3 : Common
{
... un common stuff
}但是,在可能的情况下,与继承相比,更喜欢组合。
发布于 2014-06-24 08:28:58
您可以将所有这些结构放在一个头文件(xx.h).When中,您需要这些结构,您可以包括这些头文件,例如‘include“xx.h’‘。
https://stackoverflow.com/questions/24381539
复制相似问题