避免在不同的结构中声明相同的数据类型
avoid declaring same data types in different structures
我在代码中声明了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;
}
为什么不创建通用结构呢?
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++,你可以从common struct继承:
struct struct_3 : Common
{
... un common stuff
}
你可以把所有这些结构放在一个头文件(xx.h)中。当您需要这些结构时,您可以包含这些头文件,例如' include "xx.h" '
相关文章:
- 声明"t"中的两种或多种数据类型
- 使用 CIN 和 COUT 在 C++ 中声明基本数据类型
- 如何将数据类型声明从.cpp文件传输到 .cu 文件
- 如何在C 中使用不同数据类型的类模板指针声明指针
- 类中的静态函数给出了错误:声明中的两种或多个数据类型
- typedef float _float32;的声明说明符中的两种或多种数据类型;
- 错误:'x'声明中的两个或多个数据类型
- 为什么向量的数据类型在C++的尖括号内声明
- 错误:main的声明中有两个或多个数据类型
- c++声明一个数据类型的字节数组,该数组存储在字符串中
- 指向指针的指针(C/C++)中的数据类型声明意义
- 错误:函数声明中的两种或多种数据类型
- 当数据类型未知时如何声明数组
- C++函数声明中的两个或多个数据类型的错误
- C++ 中的动态堆栈数据类型声明
- 为什么指针声明中需要数据类型
- 我们如何能够在与类具有相同数据类型的类中声明静态成员
- 使用外部类内部声明的数据类型
- 在C++中声明变量而不指定其数据类型
- g++ 似乎无法推断数据类型,除非在单独的行上声明(与 Visual C++ 不同)?