[英]Define a variable with the same type as a variable in a struct
在c ++中,我说在头文件中定义了一个结构。
******test.h***********
typedef struct mystruct{
uint8_t num;
} mystruct;
在另一个头文件中,比如myclass.h,我想定义一个与mystruct中的字段“num”具有相同类型(uint8_t)的变量。
******myclass.h***********
class myclass{
public:
??? num;
};
有没有办法定义这样的变量? 谢谢。
使用C ++ 11,您可以使用decltype
:
class myotherclass
{
public:
decltype (myclass::num) otherNum;
};
不使用C ++ 11,我这样做的典型方法就是退一步。 创建一个typedef
:
typedef uint8_t MyIntegral;
然后在两个类中使用相同的类型:
class myclass
{
public:
MyIntegral num;
};
class motherclass
{
pulic:
MyIntegral othernum;
};
这不是您要求的,但如果您可以更改myclass
的定义,您可能会发现这种方法更易于维护。
您必须在第一个类中定义类型,然后从另一个类访问它
******test.h***********
struct mystruct{
typedef uint8_t num_t;
num_t num;
};
和
******myclass.h***********
class myclass{
public:
mystruct::num_t num;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.