C++ 在类头中包含 typedef

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3653412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:23:54  来源:igfitidea点击:

include typedef inside a class header

c++

提问by Asher Saban

This is my header:

这是我的标题:

#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;


};

My question is: do I have to place the typedef inside the class block right above MessageP* _msgArr or is it OK to place it near all the #define?

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

It doesn't output compilation errors so I'm not sure about it.

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

采纳答案by kennytm

Outside of the class, that type must be referred as Timing::Message, so

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

typedef Timing::Message* MessageP;

But this is possible only after the declaration of Timing::Message, yet MessagePis used before the declaration of Timingis complete, so it's not possible.

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

Moreover, the struct is a private:member, so you can't define this outside anyway. The typedefmust be done inside the Timingclass.

而且,结构体是一个private:成员,所以无论如何你都不能在外面定义它。将typedef必须在内部完成Timing类。

class Timing {

public:

 Timing();

private:

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

 typedef Message* MessageP;   // <--


 MessageP* _msgArr;
 int _waitingMsgs;


};

You did not get compilation error probably because another Messagetype in global scope already exists.

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

回答by dirkgently

Put the typedefin a place where it makes sense.

typedef它放在有意义的地方。

Putting it at the top would mean that you inject the alias at global namespace scope. Putting it inside the class may mean that it is world viewable or viewable only to members (and/or subclasses thereof) depending on the nearest access specifier (or privateif there aren't any).

将它放在顶部意味着您在全局命名空间范围内注入别名。将它放在类中可能意味着它是世界可见的或仅对成员(和/或其子类)可见,这取决于最近的访问说明符(或者private如果没有)。

If clients of the class doesn't need to know about this alias mark it as private.

如果该类的客户不需要知道此别名,则将其标记为私有。

If you put it inside the class: Do note however that outside of the class you will need to fully qualify the name as Timing::MessagePor else need a using Timing::MessagePdirective in scope. Also, full qualification can be done only once the class has been defined (you cannot create aliases for incomplete types -- forward declaring Timingwill thus not work).

如果你把它放在类中:但是请注意,在类之外你需要完全限定名称,Timing::MessageP否则需要using Timing::MessageP作用域中的指令。此外,只有在定义了类后才能完成完全限定(您不能为不完整的类型创建别名——Timing因此前向声明将不起作用)。

class Timing {
    public:
     Timing() {}
    private:

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

 MessageP* _msgArr;
 int _waitingMsgs;

};

typedef Timing::Message* MessageP; // okay

回答by snk

It is OK to out it at the top. However, it is not a good practice to put it in global scope like the others mentioned.

在顶部取出它是可以的。但是,像其他提到的那样将其放在全局范围内并不是一个好习惯。