zoukankan      html  css  js  c++  java
  • 关于c++模板特例化测试

    模板特例化也叫模板偏特化,乍听起可能有点摸不着头脑,实际上字如其名,他就是模板的一种特例存在;比如在一个类中,模板参数接受指针类型的时候需要特殊处理,则我们可以将指针版本的那一份专门抽调出来;话不多说直接上代码

    //头文件
    #ifndef __Template_Hpp_
    #define __Template_Hpp_
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    //通用版本 template <typename T> class My_Class { public: My_Class(T key, int value) :key(key), value(value) { printf("the one test-----> "); } public: int value; T key; void print_Info() { std::cout << "the key is:" << key << " value is:" << value << std::endl; } };
    //指针版本 template <typename T> class My_Class<T*> { public: My_Class(T* key, int value) :key(key), value(value) { printf("the two test-----> "); } public: int value; T* key; void print_Info() { std::cout << "the key is:" << *key << " value is:" << value << std::endl; } }; #endif

      测试代码如下:

      

       控制台打印输出:

      

       可以看出,m_test调用的是通用版本,m_test_Ex调用的是指针版本;

  • 相关阅读:
    html-Notes3
    html-Notes2 表单
    html 笔记
    网页设计常用色彩搭配表
    css
    html-Notes
    C# 输入字符串,每3个截取一次,形成一个数组
    提高情商的好书推荐 (程序猿不仅要智商也要情商)
    PHP 学习笔记---基本语法
    php学习笔记之字符串处理
  • 原文地址:https://www.cnblogs.com/Cxiangyang/p/13708412.html
Copyright © 2011-2022 走看看