在哪里保留typedef,我应该重复typedef

A. *_* K. 3 c++ typedef

说我有三个文件

//first.h
typedef typename std::map<Vertex_t*,Vd_t> MapVertexVd_t;
//the graph class and ...
//other useful things related to a graph class

//second.h
#include "first.h"
class analyzeGraph {
MapVertexVd_t mapVertexVd;

public:
void fillMap();
};

//second.cpp
#include "second.h"
void analyzeGraph::fillMap()
{
//
}
Run Code Online (Sandbox Code Playgroud)

现在问题是我应该放置:

typedef typename std::map<Vertex_t*,Vd_t> MapVertexVd_t;
Run Code Online (Sandbox Code Playgroud)

在文件second.h和second.cpp中为了清楚起见.我觉得我会在一段时间后感到困惑,因为我的代码中有太多的typedef和typename.

Jam*_*mes 5

我发现根据具体情况,这些方法很有效:

  • 对于在很多文件中使用的typedef,将它们放在共享头文件中(可能带有前向声明等)
  • 对于仅由少数函数或方法使用的typedef(在声明中),将它们放在第一个相关声明之前,并确保函数一起声明.
  • 对于用于类或函数中的数据的typedef,将它们放在相关范围的开头,以便读取代码的人首先看到它们.(对于类,使typedef的可见性尽可能低 - 私有typedef应保持私有.)