c++ - 在类头中包含 typedef

标签 c++

这是我的标题:

#ifndef TIMING_H
#define TIMING_H

#define MAX_MESSAGES 1000
typedef Message* MessageP; //inside the class?

class Timing {

public:

 Timing();

private:

 struct Message {
  Agent *_agent;
  double _val;
 };

 MessageP* _msgArr;
 int _waitingMsgs;


};

我的问题是:我必须将 typedef 放在 MessageP* _msgArr 正上方的类 block 中,还是可以将它放在所有#define 附近?

它不输出编译错误所以我不确定。

最佳答案

在类之外,该类型必须被称为Timing::Message,所以

typedef Timing::Message* MessageP;

但这只有在Timing::Message声明之后才有可能,而MessageP是在Timing声明完成之前使用的,所以这是不可能的。

此外,该结构是 private: 成员,因此无论如何您都不能在外部定义它。 typedef 必须在 Timing 类中完成。

class Timing {

public:

 Timing();

private:

 struct Message {
  Agent *_agent;
  double _val;
 };

 typedef Message* MessageP;   // <--


 MessageP* _msgArr;
 int _waitingMsgs;


};

您没有收到编译错误可能是因为全局范围内的另一个 Message 类型已经存在。

关于c++ - 在类头中包含 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3653412/

相关文章:

C++ 如何检查 Outlook 进程是否已经在运行

c++ - C++11 中的 lambda 表达式是什么?

c++ - 如何将 AnsiString 转换为 char?

c++ - 两个整数之间的随机函数生成器 C++

c# - 更改未反射(reflect)在 C# 代码和 C++ 代码之间传递的变量值中

c++ - 确定正确的 protobuf 类

c++ - C++ 中的自定义分配器有什么问题?

c++ - 如何使用 keystates SDL 检查 key 是否已被释放

c++ - 如何使用 QtDbus 注册接口(interface)和注册方法?

c++ - isFollowingCamelCaseConventionInCPlusPlus more_import_than_readability?