本文主要是介绍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()
{}
这篇关于c++的类模块,具体化和继承关系的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!