C++ 泛型编程详解

这一篇介绍一下 C++ 编程中与面向对象并列的另一大分支――泛型编程,这一篇主要介绍函数模板、类模板和成员模板三大部分,需要的朋友可以参考下

泛型编程与面向对象编程的目标相同,即使重用代码和抽象通用概念的技术更加简单。但是面向对象编程强调编程的数据方面,泛型编程强调的是独立于特定数据类型。

这一篇介绍一下 C++ 编程中与面向对象并列的另一大分支――泛型编程,这一篇主要介绍函数模板、类模板和成员模板三大部分

如有侵权,请联系删除,如有错误,欢迎大家指正,谢谢

泛型编程

模板是泛型编程的一种重要思想,STL(Standard Template Library,标准模板库)是采用模板实现的一个实例
函数模板

对比函数重载(同一作用域内函数名相同,参数列表不同的函数),函数模板只需要一个函数就实现了函数重载的部分功能(参数个数相同类型不同,函数重载需要定义多个同名参数列表不同的函数)

 template // 这也可以写 template 此处的 class 和 typename 作用相同 void tfunc(T& t, Y& y) { cout << t << " " << y << endl; } int n = 2; double d = 2.1; tfunc(n, d); // 运行结果:2 2.1

函数模板具体化,函数模板具体化就是将某一(某几)个要处理的类型单独处理,需要单独写一个实现,形式是 template<> void fun(type& t);,函数模板的具体化和普通函数可以同时存在,调用顺序是 普通函数 > 函数模板具体化 > 模板函数

 // ====== 测试一:函数模板针对特殊数据类型具体化 ====== struct Node { int val; Node* next; }; // 函数模板 template void tfunc(const T& t) { cout << "template: " << t << endl; } // 函数模板具体化(用于处理Node类型数据) template<> void tfunc(const Node& node) { cout << "template: " << node.val << endl; } // 函数模板具体化(用于处理int类型数据) template<> void tfunc(const int& n) { cout << "template: " << n << endl; } // 普通函数 void tfunc(const int& n) { cout << "tfunc(): " << n << endl; } double d = 2.1; tfunc(d); // 函数模板未具体化double类型函数,调用模板 Node node{ 2, nullptr }; tfunc(node); // 函数模板具体化Node类型函数,调用函数模板的具体化 int n = 2; tfunc(n); // 函数模板具体化int类型函数,也存在普通函数,调用普通函数 // ====== 测试二:函数模板部分具体化 ====== template void tfunc(T1 t1, T2 t2) { cout << typeid(T1).name() << " and " << typeid(T2).name() <<": " << t1 << " " << t2 << endl; } template void tfunc(T1 t1, int i) { cout << typeid(T1).name() << " and " << "int: " << t1 << " " << i << endl; } template void tfunc(long l, T2 t2) { cout << "long and " << typeid(T2).name() << ": " << l << " " << t2 << endl; } template<> void tfunc(short l, int i) { cout << "long and int: " << l << " " << i << endl; } // 分别调用以上四个模板函数 tfunc(char('c'), char('c')); tfunc(char('c'), int(10)); tfunc(long(10), char('c')); tfunc(short(10), int(10));

函数模板实例化,让编译器生成指定类型的函数定义,不用写函数的实现,形式是 template void fun(type& t);

 // 函数模板 template void tfunc(const T& t) { cout << "template: " << t << endl; } // 函数模板实例化,不用写函数的实现,编译器会生成该类型的模板具体化函数 template void tfunc(const char& c); 

类模板

类模板可以指定默认模板参数(函数模板不可以),跟函数参数的默认值一样,必须从右向左连续赋值默认类型,如果实例化对象时又传递了类型,默认类型会被覆盖掉,跟函数参数是一样的
创建对象时需要传递模板参数列表,模板参数列表加在类名后面 ClassName classN; 如果类的模板参数列

表有默认值,可以不传模板参数,但一定要加 <> 如 ClassName<> classN; 创建堆区对象的时候,所有的类名称后面都要加模板参数列表,如 ClassName* classN = new ClassName; 除了类内,其他地方出现 ClassName 的地方一般都要加模板参数列表

 template // 此处指定了模板默认参数,部分指定必须从右到左指定 class Test { public: Test(T t, Y y) : t(t), y(y) { } void tfunc(); private: T t; Y y; }; template // 类模板的函数在类外实现,需要加上模板参数列表,但不需要加指定的默认模板参数 void Test::tfunc() { // 类外使用Test需要加模板参数 cout << t << " " << y << endl; } int n = 2; double d = 2.1; Test test(n, d); // 此处如果使用默认模板参数可定义为 Test<> test(int(2), char('a')); test.tfunc(); // 运行结果:2 2.1

类模板的继承,类模板被继承后参数的传递方式主要有两种,一种是直接在子类继承父类的时候,为父类指定固定的类型,二是通过子类模板参数列表传递

 // ====== 测试一 ====== template class A { public: A(T t, Y y) { } }; class Test : public A { // 父类是类模板,子类是普通类 public: Test() : A(2, 2.1) { } }; Test(); // ====== 测试二 ====== template class A { public: A(T t) { } }; template class Test : public A { public: Test(X x, Z z, P p) : A(x) { } }; Test(int(2), double(2.1), char('a'));

类模板的多态,在创建对象时,分为子类没有模板(CFather*cf = new CSon;)和子类有模板(CFather *cf = new CSon)两种,子类和父类的模板参数列表可以不一样,但一定要对应好

 // ====== 测试一 ====== template class A { public: virtual void tfunc(T t, Y y) = 0; }; class Test : public A { public: virtual void tfunc(int n, double d) { cout << n << " " << d << endl; } }; // 父类是类模板,子类是普通类,在多态情况下父类需要指定模板参数,子类就不用了 A* a = new Test; a->tfunc(2, 2.1); // 运行结果:2 2.1 // ====== 测试二 ====== template class A { public: virtual void tfunc(T t, Y y) = 0; }; template class Test : public A { public: virtual void tfunc(X x, P p) { cout << x << " " << p << endl; } }; // 父类是类模板,子类是类模板,在多态情况下父类和子类都需要指定模板参数 A* a = new Test; a->tfunc(2, 2.1); // 运行结果:2 2.1

类模板具体化,类模板的具体化分为部分具体化和全部具体化两种

 template class Test { public: Test() { cout << "T1 and T2" << endl; } }; // 部分具体化 template class Test { public: Test() { cout << "T1 and int" << endl; } }; // 部分具体化 template class Test { public: Test() { cout << "long and T2" << endl; } }; // 全部具体化 template<> class Test { public: Test() { cout << "long and int" << endl; } }; // 分别创建上面四个类 Test(); Test(); Test(); Test();

成员模板

成员模板简单说就是模板中的模板

 class Base1 { }; class Base2 { }; class Test1 : public Base1 { }; class Test2 : public Base2 { }; template class Pair { public: T1 t1; T2 t2; Pair(T1 t1, T2 t2) : t1(t1), t2(t2) { } // 类模板中的成员模板 template Pair(const Pair& pair) : t1(pair.t1), t2(pair.t2){ } }; Pair(Pair(new Test1, new Test2));

如果未特殊说明,以上测试均是在win10 vs2017 64bit编译器下进行的

总结

以上所述是小编给大家介绍的C++ 泛型编程,希望对大家有所帮助!

以上就是C++ 泛型编程详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言