c++ - 如何避免 typedef?

标签 c++ templates typedef

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/

相关文章:

c++ - 如何使用 C++ 运行批处理文件?

c++ - CString 到 TCHAR

c++ - 模板友元

c++ - 如何使函数参数容器独立

php - Twig 检查多个值

c - 返回值的枚举或 typedef 枚举

c - 如何理解以下有关 C 中 typedef 的代码

c++ - 模板 + Typedef C++

c++ - 调用反向函数的代码无法在 Ubuntu 18 上的 g++ 或 clang++ 上编译,但神秘地在 mac osx 上运行

c++ - 如何使用 C/C++ 有效地加入巨大的 csv 文件(100 0's of columns x 1000' 行)?