资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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类型函数,也存在普通函数,调用普通函数// ====== 测试二:函数模板部分具体化 ======templatevoid tfunc(T1 t1, T2 t2) { cout << typeid(T1).name() << " and " << typeid(T2).name() <<": " << t1 << " " << t2 << endl;}templatevoid tfunc(T1 t1, int i) { cout << typeid(T1).name() << " and " << "int: " << t1 << " " << i << endl;}templatevoid 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< typename T > classN; 如果类的模板参数列

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

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

// ====== 测试一 ======templateclass A {public: A(T t, Y y) { }};class Test : public A { // 父类是类模板,子类是普通类public: Test() : A(2, 2.1) { }};Test();// ====== 测试二 ======templateclass A {public: A(T t) { }};templateclass 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;};templateclass 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

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

templateclass Test {public: Test() { cout << "T1 and T2" << endl; }};// 部分具体化templateclass Test {public: Test() { cout << "T1 and int" << endl; }};// 部分具体化templateclass 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++泛型编程是什么-创新互联
浏览地址:http://cdkjz.cn/article/dcihoh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220