[英]Should this bunch of typedefs be private or public?
我正在写一个代表图形的类,所以我写了下面的标题
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);
};
我对上面的代码有一些疑问:
在类的public
接口中使用的任何typedef
都应该在类的public
部分中。 休息应该是private
。
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 ]
2.没关系,因为你可以使某些类型有意义且容易理解。
private
,则无法在课堂外使用它们。 我想这不是你想要的,特别是如果typedef
ed名称在你的公共接口中显示为参数类型。 如果限制在类中使用typedef
ed名称,则将其private
。 typedef
将类型重命名为域级别的更可用/易于理解的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.