本文链接: https://blog.youkuaiyun.com/Mr_scott_j/article/details/108943644
以从Graph类派生出的UndiGraph类为例:
class Graph//基类
{
protected:
int maxVertexes;
int Vertex_num;
int Edge_num;
bool Weight;
bool Directed;
bool Vertex_value;
public:
//static const E maxWeight = INF;
Graph(int size = 30);
Graph(const Graph& g);//copy
~Graph() {
}
int getnumVertexes()
{

本文探讨了在C++中,当从模板类Graph派生出UndiGraph类时,如何在派生类成员函数中正确访问基类的数据成员Vertex_num。由于编译器在模板类未被实例化时无法确定其具体类型,因此可能导致找不到成员的错误。解决方案包括使用`this->`前缀或通过`using`声明式明确指定成员来自基类。这种做法是对编译器的一种承诺,确保所有特例化版本都支持接口,否则编译器将报错。
最低0.47元/天 解锁文章
811

被折叠的 条评论
为什么被折叠?



