模板类的使用

您所在的位置:网站首页 模板的使用方法 模板类的使用

模板类的使用

2024-07-11 16:14| 来源: 网络整理| 查看: 265

1.目的:使用模板的目的就是能够让程序员编写与类型无关的代码。

   注:模板的声明或定义只能在全局,命名空间或类范围内进行。

2.通式:class 或 typename

类:template   class 类名 { ... };函数:template  返回类型 函数名(参数列表)    { 函数体 }

3.编译器:在代码中包含模板本身不生成函数定义,只有使用模板为特定类型时才生成函数定义。调用时,生成定义。

4.类型形参和非类型形参

类型形参 #ifndef _TEMPLATE_H__ #define _TEMPLATE_H__ template class CTemplate { public: void Temp(T& a); public: CTemplate() {} ~CTemplate() {} }; template void CTemplate::Temp(T&& a)/*使用右值引用*/ { printf("test:%d----\n", a); } #endif /*_TEMPLATE_H__*/

  调用实例:

#include "stdafx.h" #include "template.h" int _tmain(int argc, _TCHAR* argv[]) { CTemplate objTemplate; int i = 4; objTemplate.Temp(i); objTemplate.Temp(3);//如果不使用右值引用,编译器在这里报错 return 0; } 非类型形参 #ifndef _TEMPLATE_H__ #define _TEMPLATE_H__ template class CTemplate { private: T1 array[iMax]; public: void Add(T1 const& element, T2& index); void Print(); public: CTemplate(); ~CTemplate() {} }; template CTemplate::CTemplate() { for (auto auNum = 0; auNum < iMax; auNum++) { array[auNum] = 0; } } template void CTemplate::Add(T1 const& element, T2& index) { if (index >= iMax) index = iMax - 1; //index = min(index, iMax); array[index] = element; } template void CTemplate::Print() { for (auto auNum = 0; auNum < iMax; auNum++) { printf("array[%d]:%d\n", auNum, array[auNum]); } } #endif /*_TEMPLATE_H__*/

调用实例:

#include "stdafx.h" #include "template.h" int _tmain(int argc, _TCHAR* argv[]) { CTemplate objTemplate; int index = 0; objTemplate.Add(2, index); index = 2; objTemplate.Add(3, index); index = 5; objTemplate.Add(5, index); index = 13; objTemplate.Add(8, index); objTemplate.Print(); return 0; }

结果:

5.模板类的继承:必须规定子类的类型

#ifndef _TEMPLATE_H__ #define _TEMPLATE_H__ template class CBase { public: void Print(T& a); public: CBase() {} ~CBase() {} }; template void CBase::Print(T& a) { printf("fuction:base:%lf\n", a); } template class CTemplate : public CBase { public: void Print(T& a); public: CTemplate() {} ~CTemplate() {} }; template void CTemplate::Print(T& a) { printf("function:template:%d\n", a); } #endif /*_TEMPLATE_H__*/

调用实例:

#include "stdafx.h" #include "template.h" int _tmain(int argc, _TCHAR* argv[]) { CBase objBase; CTemplate objTemplate; int iData = 2; double dData = 1.1; objBase.Print(dData); objTemplate.Print(iData); return 0; }

结果:

6.类型转换:decltype或auto, auto更符合编码习惯

#ifndef _TEMPLATE_H__ #define _TEMPLATE_H__ template class CBase { public: void Print(T1 a, T2 b); public: CBase() {} ~CBase() {} }; template void CBase::Print(T1 a, T2 b) { decltype (a + b) ab = a + b; auto auAb = a + b; printf("fuction:base:%lf %lf\n", ab, auAb); } #endif /*_TEMPLATE_H__*/

调用实例

#include "stdafx.h" #include "template.h" int _tmain(int argc, _TCHAR* argv[]) { CBase objBase; objBase.Print(2, 1.1); return 0; }

结果:

7.模板函数的使用(可以简化重载函数)

   test.h

#pragma once #include class CTest { public: void SetData(int iData); template void GetSourse(T& t) { auto data = t; } public: CTest() = default; ~CTest() = default; };

  test.cpp

#include "test.h" void CTest::SetData(int iData) { printf("iData:%d\n", iData); }

main.cpp

#include #include "test.h" int main() { CTest objTest; int iData = 4; float fData = 1.0; double dfData = 5.0; objTest.GetSourse(iData); objTest.GetSourse(fData); objTest.GetSourse(dfData); objTest.SetData(4); std::cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3