这一堆typedef应该是私有的还是公共的?

vor*_*192 5 c++ typedef class graph

我正在写一个代表图形的类,所以我写了下面的标题

class Graph {
public:
  Graph(); 
  Graph(int N);

  void addVertex();
  void addEdge(VertexNum v1, VertexNum v2, Weight w);


  std::pair<PathLength, Path> shortestPath
    (const VerticesGroup& V1, const VerticesGroup& V2);


private:
  typedef int                           VertexNum;
  typedef int                           Weight;
  typedef std::pair<VertexNum, Weight>  Edge;
  typedef std::vector<Edge>             Path;
  typedef size_t                        PathLength;
  typedef std::vector<VertexNum>        VerticesGroup;


  std::vector<std::list<Edge> > adjList;

  bool incorrectVertexNumber(VertexNum v);
};
Run Code Online (Sandbox Code Playgroud)

我对上面的代码有一些疑问:

  1. 我应该声明一堆typedef是public还是private?
  2. 这是一种常规做法,当一个type将一个类型解析为不同的同义词时(比如typedef int VertexNum; typedef int Weight;)?

R S*_*ahu 8

在类typedefpublic接口中使用的任何应该在类的public部分中.休息应该是private.


Jer*_*ain 6

1. C++中的访问控制纯粹适用于名称.ISO/IEC 14882:2011 11 [class.access]/4中有一个注释和示例,表明这是意图.

[...] [ Note: Because access control applies to names, if access control is applied to a typedef name, only the accessibility of the typedef name itself is considered. The accessibility of the entity referred to by the typedef is not considered. For example,

class A {
  class B { };
public:
  typedef B BB;
};

void f() {
   A::BB x; // OK, typedef name A::BB is public
  A::B y; // access error, A::B is private
}
—end note ]
Run Code Online (Sandbox Code Playgroud)

2.没关系,因为你可以使某些类型有意义且容易理解.