c++的类模块,具体化和继承关系

1.类模版及它的全具体化,半具体化

template<typename Tl,typename T2)
class Test{
public:
T1 m_a;
T2 m_b;
Test(T1 a,T1 b):m_a(a),m_b(b)
 {
 qDebug()<< "m_a"<<m_a;
 }
/类模板全具体化
template<>
class Test<int,string>
{
public:
int m_a;
string m_b;
Test(int a, string b):m_a(a),m_b(b)
{
 qDebug()<< "m_a"<<m_a;
}
//类模板半具体化
template<typename T>
class Test<T,string>
{
public:
T m_a;
string m_b;
Test(T a, string b):m_a(a),m_b(b)
{
qDebug()<<"m_a"<<m_a;
}
}

2.模板之间的继承(3种)关系 。(照顾好基类的构造函数)

2.1模版类继承普通类

//普通类
class AA
{
public:
int m_a;
string m b;
AA(int a,string b):m_a(a),m_b(b)
{
qDebug()<<"m_a"<<this->m_a;
}	
}
//类模板继承普通类
template<typename T1,typename T2>
class Test:public AA
{
public:
T1 m_a;
T1 m_b;
Test(Tl a,Tl b,int c,string d):m_a(a),m_b(b),AA(c,d)
{
qDebug()<< "m_a"<<m_a;
}

怎么使用

int main(int argc, char *argv[])
Test<int,double> ts(7,9,3,"birdsB");

2.2普通类继承模版类

//先写模版类
template<typenameTl,typenameT2>class BB{
public:
})
Tl m_a;T2 m b;
BB(T1 a,T2 b):m_a(a),m_b(b){qDebug()<<"m_a"<<this->m_a;}

//写普通类继承模版类
template<typename Tl,typename T2>class AA:public BB<T1.T2>{
public:
int m a;
string m b;
AA(int a,string b,Tl c,T2 d):m_a(a),m_b(b),BB<T1,T2>(c,d)
{
qDebug()<<"m_a"<<this->m_a;
}

使用方法

int main(int argc, char *argv[])
{
AA<int,string> a(4,"he",5,"dgf");
}

2.3类模板继承类模板

//模版类:BB
template<typenameTl,typenameT2>class BB
{
public:
Tl m_a;
T2 m b;
BB(T1 a,T2 b):m_a(a),m_b(b)
{
qDebug()<<"m_a"<<this->m_a
}
}
模版类CC继承BB
template<typename Tl,typename T2,typename T3,typename T4)
class cc:public BB<T3,T4>
{
public:
T1 m_a;
T2 m_b;
CC(T1 a,T2 b,T3 c,T4 d):m_a(a),m_b(b),BB<T3,T4>(c,d)
{
qDebug()<<"m_a"<<this->m_a;
}

void show();

怎么使用

int main(int argc, char *argv[])
{
CC<int,string,double,string> a(4,"he",5.0,"dgf");
}

模版类继承模版类,成员变量类外实现

template<typename Tl,typename T2,typename T3,typename T4)

void BB<T1,T2,T3,T4>::show()
{

}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值