相关疑难解决方法(0)

为什么我必须通过this指针访问模板基类成员?

如果下面的类不是模板,我可以简单地xderived课堂上.但是,使用下面的代码,我必须使用this->x.为什么?

template <typename T>
class base {

protected:
    int x;
};

template <typename T>
class derived : public base<T> {

public:
    int f() { return this->x; }
};

int main() {
    derived<int> d;
    d.f();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates c++-faq

184
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×1

c++-faq ×1

inheritance ×1

templates ×1