C++模板使用

模板

模板让程序员能够定义一种适用于不同类型对象的行为(有点像java中的泛型),模板是类型安全的。

模板语法:

/* 
	关键字template标志着模板声明的开始,接下来的模板参数列表:关键字typename,后面的T1是模板参数,T2的默认值是T1
	针对对象实例化模板时,使用对象的类型代替这里的T1、T2
*/
// 1. 模板函数 
template <typename T1, typename T2 = T1> 
bool TemplateFunction(const T1& param1, const T2& param2);

//2. 模板类
template <typename T1, typename T2 = T1>
class MyTemplate 
{
    
     
private: 
 T1 member1; 
 T2 member2; 
public: 
 T1 GetObj1() {
    
    return member1; } 
 // ... other members 
}; 

模板函数

#include <iostream>
#include <string>
using namespace std;

template <typename Type>
const Type& GetMax(const Type& value1, const Type& value2)
{
    
    
    if (value1 > value2)
    {
    
    
        return value1;
    }
    else
    {
    
    
        return value2;
    }
}

template <typename Type>
void DisplayComparision(const Type& value1, const Type& value2)
{
    
    
    cout << "GetMax(" << value1 << ", " << value2 << ")  =  ";
    cout << GetMax(value1, value2) << endl;
}

int main()
{
    
    
    int Int1 = -101, Int2 = 2011;
    DisplayComparision <int> (Int1, Int2); // 隐式指定:DisplayComparison(num1, num2),下同
    // GetMax(-101, 2011)  =  2011

    double d1 = 3.14, d2 = 3.1416;
    DisplayComparision <double> (d1, d2);
    // GetMax(3.14, 3.1416)  =  3.1416

    string Name1 = "Jack";
    string Name2 ("John");
    
    DisplayComparision<string>(Name1, Name2);
    // GetMax(Jack, John)  =  John
    return 0;
}

模板类

#include <iostream>
using namespace std;

// 这里定义了两个参数的模板,并且带有各自默认值
template <typename T1 = int, typename T2 = double>
class HoldPair
{
    
    
private:
    T1 Value1;
    T2 Value2;
public:
    HoldPair (const T1& inputValue1, const T2& inputValue2)
    {
    
       
        Value1 = inputValue1;
        Value2 = inputValue2;
    }

    const T1& getFirstValue () const
    {
    
    
        return Value1;
    }
    const T2& getSecondValue () const
    {
    
    
        return Value2;
    }
};
int main()
{
    
    
    HoldPair <> mIntFloatPair (300, 10.09);
    HoldPair <short, char*> mShortStringPair (25, "I am wkm");

    cout << "The first template class:" << endl;
    cout << "Value1: " << mIntFloatPair.getFirstValue() << endl;
    cout << "Value2: " << mIntFloatPair.getSecondValue() << endl;
    /*
        The first template class:
        Value1: 300
        Value2: 10.09
    */
    cout << "The second template class:" << endl;
    cout << "Value1: " << mShortStringPair.getFirstValue() << endl;
    cout << "Value2: " << mShortStringPair.getSecondValue() << endl;
    /*
        The second template class:
        Value1: 25
        Value2: I am wkm
    */
    return 0;
}

关于模板类的静态成员

#include <iostream>
using namespace std;

template <typename T>
class TestStatic
{
    
    
public:
    static int StaticValue;
};

/*
    初始化模板类的静态成员:
    template<template parameters> StaticType 
    ClassName<Template Arguments>::StaticVarName; 
*/
template <typename T> int TestStatic<T>::StaticValue;

int main()
{
    
    
    TestStatic<int> Int_Year;
    cout << "Setting StaticValue for Int_Year to 2011" << endl;
    Int_Year.StaticValue = 2011;
    TestStatic<int> Int_2;

    TestStatic<double> Double_1;
    TestStatic<double> Double_2;
    cout << "Setting StaticValue for Double_2 to 1011" << endl;
    Double_2.StaticValue = 1011;

    cout << "Int_2.StaticValue = " << Int_2.StaticValue << endl;
    // Int_2.StaticValue = 2011
    cout << "Double_1.StaticValue = " << Double_1.StaticValue << endl;
    // Double_1.StaticValue = 1011
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43869409/article/details/129549002