template <class VertexType>
class GraphMatrix
{
};
template <class GraphType>
class Graph
{
typedef typename GraphType::VertexType VertexType;
GraphType* graph_;
void addVertex(VertexType vertex)
{
}
//...
};
int main()
{
Graph <GraphMatrix <int> > graph;
return 0;
}
看看我是否将 typedef VertexType VertexType;//A
行添加到 GraphMatrix
class
此代码将通过编译并可以运行,否则它给出了编译错误。错误:C2039:“VertexType”:不是“GraphMatrix”的成员。我的问题是“有没有一种方法(语法)可以使上面的代码工作而无需添加这条愚蠢的行 //A
”?
最佳答案
您始终可以编写 VertexType
的完全限定名称,typedef
只是您的快捷方式,它将该名称纳入范围并让您编写更清晰的代码。
所以,不在这种情况下,没有别的办法。
然而,当使用继承时,您可以使用 BaseClass::something
将其引入范围。
关于c++ - 如何避免 typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4422276/