`
javaFlood
  • 浏览: 10143 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

继承c++ template类的时候找不到基类protected成员定义

阅读更多
翻了好多地方,有人说在成员前面加this->可以解决,果然可以,但一直不明白原因,
代码:
#include <iostream>
#include <map>
#include <string>
#include <deque>
#include <vector>

using namespace std;

template <class T>
class b {
public:
    b(){}
    b(T i){
        x.push_back(i);
    }  
    void print(){
        if(x.size()>0)
            cout<<x[0]<<endl;
    }  
protected:
    vector<T> x;
};

template <class T>
class e: public b<T>{
public:
    e(T i):b<T>(i){
    }  
    void print(){
        if(this->x.size()>0)//这里要加this->或者b<T>::
            cout<<this->x[0]<<endl;
    }  
};
int main()
{
    e<int> a(1);
    a.print();
}
在http://forum.ubuntu.org.cn/viewtopic.php?t=240044上有人解释了说是编译器偷懒的原因
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics