首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-09-05 
继承c++ template类的时候找不到基类protected成员定义翻了好多地方,有人说在成员前面加this-可以解决,果

继承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;
    }  
public:
    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上有人解释了说是编译器偷懒的原因

热点排行