我有一个tempated基类检查和公开派生类childcheck。基类也有一个部分特殊化,但我从一般模板类继承了子类检查类(而不是从类检查的局部特化)。当我打电话从派生类的初始化列表基类的构造函数,编译器会发出错误,现在如果我删除类检查的部分特则编译器会发出任何错误,所以这里是代码
模板和继承问题!
#include<iostream.h>
template<class t>
class check
{
t object;
public:
check(t element);
};
template<class t>
check<t>::check<t>(t element)
{
cout<<"base class constructor"<<endl;
}
//partial specialization
template<class t>
class check<t*>
{
int objectsize;
t* object;
public:
check(t*,int);
t* getelement()const;
~check();
};
template<typename t>
check<t*>::check<t*>(t* ptr,int size)
{
cout<<"\n partial specialization constructor";
}
//derived class
template< class t>
class childcheck:public check<t>
{
t chobject;
public:
childcheck(t);
t getobject()const;
};
template<class t>
childcheck<t>::childcheck(t element):check<t>(element+1)
{
cout<<"derived class constructro"<<endl;
}
//and this is the main function
main()
{
int* ptr;
int x=2;
ptr=&x;
childcheck<int*> object(ptr);
system("pause");
}
是你的权利,非常感谢你 –