翻了好多地方,有人说在成员前面加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上有人解释了说是编译器偷懒的原因
代码:
#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上有人解释了说是编译器偷懒的原因