1:默认模板参数就是在类模板定义时设置类型形式参数表中的一个类型参数的默认值,该默认值是一个数据类型。有了默认的数据类型参数后,在定义模板的新类型时就可以不进行指定。代码如下:

C++入门经典-例9.4-默认模板参数C++入门经典-例9.4-默认模板参数
// 9.4.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T1,class T2 = int>
class MyTemplate
{
    T1 t1;
    T2 t2;
public:
        MyTemplate(T1 tt1,T2 tt2)
        {t1=tt1;t2=tt2;}
        void display()
        {
         cout<< t1 << ' ' << t2 << endl;
    }
};
void main()
{
    int a=123;
    double b=3.1415;
    MyTemplate<int ,double> mt1(a,b);
    MyTemplate<int> mt2(a,b);
    mt1.display();
    mt2.display();
}
View Code

运行结果:

C++入门经典-例9.4-默认模板参数

 

相关文章:

  • 2021-08-29
  • 2021-10-12
  • 2021-11-19
  • 2022-02-18
猜你喜欢
  • 2021-06-01
  • 2021-08-04
  • 2022-01-16
  • 2021-06-25
相关资源
相似解决方案