C++ 模板和继承问题!

C++ 模板和继承问题!,c++,C++,我有一个临时基类检查和公开派生的类childcheck。基类也有一个部分专门化,但我从通用模板类继承了childcheck类(而不是从类检查的部分专门化)。当我从派生类的初始化列表中调用基类的构造函数时,编译器会给出错误,现在如果我删除类检查的部分专门化,那么编译器不会给出错误,所以下面是代码 #包括 模板 课堂检查 { t对象; 公众: 检查(t元件); }; 模板 检查::检查(t元素) { coutcheck::check(t*,int);c'tor接受两个参数,但您从派生类初始化列表中

我有一个临时基类检查和公开派生的类childcheck。基类也有一个部分专门化,但我从通用模板类继承了childcheck类(而不是从类检查的部分专门化)。当我从派生类的初始化列表中调用基类的构造函数时,编译器会给出错误,现在如果我删除类检查的部分专门化,那么编译器不会给出错误,所以下面是代码

#包括
模板
课堂检查
{
t对象;
公众:
检查(t元件);
};
模板
检查::检查(t元素)
{

coutcheck::check(t*,int);
c'tor接受两个参数,但您从派生类初始化列表中将其称为
check(element+1)
(t==int*,因此部分专门化是实例化的)。

更一般地说,您面临着专门化的常见问题

当您编写模板类的专门化时,通常必须注意其接口,并确保它与您专门化的模板类的接口相匹配,否则会出现这些令人讨厌的意外情况

当然,在某些情况下,专门化的目标是提供不同的行为,某些操作不再有意义,但这样您就失去了泛型编程的好处,因为您不能再像对待任何其他实例一样对待模板的任何实例,并且必须为专门化编写特殊案例电子接口不同

你刚刚发现这一点都不好玩;)

#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");
}