gpt4 book ai didi

c++ - 模板和继承问题!

转载 作者:行者123 更新时间:2023-11-30 03:11:20 24 4
gpt4 key购买 nike

我有一个模板基类检查和公共(public)派生类子检查。基类也有部分特化,但我从通用模板化类(而不是类检查的部分特化)继承了 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");
}

最佳答案

check<t*>::check(t*,int); c'tor 有两个参数,但你称它为 check<t>(element+1)来自派生类初始化列表(使用 t==int*,因此部分特化被实例化)。

关于c++ - 模板和继承问题!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2350997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com