关于C++模板的困惑
今天无聊中写了以下一段代码:
#include <iostream>
template<class TC>
class other
{
public:
other(){};
~other(){};
TC *ptc;
void eat(){
ptc->catrun(5); //困惑?
}
};
template<class TC>
class base
{
public:
base(){};
virtual ~base(){};
other<TC> o1; // 模板类成员
};
class derived : public base<derived>
{
public:
derived(){};
~derived(){};
void catrun(int i){
int j = 0;
j += i;
std::cout << "j : " << j << std::endl;
}
};
int main()
{
derived derived1;
derived1.o1.eat();
return 0;
}
在实例化derived之后,base类和其成员O1也被实例化了,
但我的困惑在于,此时O1中的指针ptc并没有初始化,但是程序执行到语句
ptc->catrun(5);
时不会报错,且能输出正确的结果
这是为什么?