#include "stdafx.h"
#include "iostream"
#include <ctime>
using namespace std;

//全局常量size=4
const int size=4;

template <typename T>
class MyClass
{
public:
    MyClass(T* p)
    {
        for (int i = 0; i < size;i++)
        {
            array[i] = p[i];
        }
    }
    void print();
private:
    T array[size];
};
template <typename T>
//模板类函数定义需要加MyClass<T>
void MyClass<T>::print()
{
    for (int i = 0; i < size;i++)
    {
        cout << array[i] << '\t';
    }
}
int _tmain()
{
    int intArray[size] = { 1, 2, 3, 4 };
    //MyClass <double> obj(intArray);
    //double需要改成int,类的构造函数的参数要和模板类参数一致
    MyClass <int> obj(intArray);
    obj.print();
    cout << endl;
    return 0;
}

template <typename T>模板类定义的更多相关文章

  1. C++ 类模板一(类模板的定义)

    //类模版语法 #include<iostream> using namespace std; /* 类模板和函数模板深入理解 1.编译器并不是把函数模板处理成能处理任何类型的函数 2.编 ...

  2. C++:类模板与模板类

    6.3 类模板和模板类 所谓类模板,实际上是建立一个通用类,其数据成员.成员函数的返回值类型和形参类型不具体指定,用一个虚拟的类型来代表.使用类模板定义对象时,系统会实参的类型来取代类模板中虚拟类型从 ...

  3. C++ template —— trait与policy类(七)

    第15章 trait与policy类---------------------------------------------------------------------------------- ...

  4. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

  5. C++模板类中友元函数的写法

    首先,已声明好的类Triangle file://Triangle.h template<class T> class Triangle{ public: Triangle(T width ...

  6. C++高级 STL——模板函数、模板类

    1.模板函数 // 定义 template <class T> Max(T &t1, T &t2) { return ((t1 > t2) ? t1 : t2); } ...

  7. [转]引用模板类中定义的类型(用typedef或using)以及auto、decltype、typename的使用

    一.背景 使用typedef或者using定义类型别名是非常常见的手段,在c++里面,有时为了封装性,模块性等原因还会在某一个namespace或者class内部定义类型别名. 最近在写c++代码的时 ...

  8. 如何在动态链接库dll/so中导出自定义的模板类template class | how to implement a template class with c++ and export in dll/so

    本文首发于个人博客https://kezunlin.me/post/4ec4ae49/,欢迎阅读最新内容! how to implement a template class with c++ and ...

  9. C++ and Java template class and function 模板类和模板函数

    在C++和Java的泛式编程中,模板template的使用是必不可少的,但是Java中没有template关键字,所以两者的写法还是有些许区别的,请参见如下代码: Java的模板 // Java pu ...

随机推荐

  1. 【IntelliJ IDEA】在idea上安装使用svn

    1.在电脑上安装SVN 下载地址:64位SVN下载 然后一路next,安装完成即可. 如果忘记勾选第二个,可以重新点击安装包  重新安装,然后选择modify,然后勾选command line cli ...

  2. c#中开发ActiveX的学习笔记【转】

    http://www.cnblogs.com/yjmyzz/archive/2009/12/14/1623396.html 1.为什么要用ActiveX? 网页本身的功能是有限的,要想实现一些网页本身 ...

  3. jquery中美元符号($)命名冲突

    http://blog.csdn.net/shanshan209/article/details/6341727 在Jquery中,$是JQuery的别名,所有使用$的地方也都可以使用JQuery来替 ...

  4. SwitchyOmega 代理设置

    1.SwitchyOmega官网 https://www.switchyomega.com/ 2.下载插件 https://www.switchyomega.com/download.html 3.配 ...

  5. gulp-gulpfile.js语法说明

    关于gulpfile文件: 直接上代码吧!! /*! * gulp * $ npm install gulp gulp-ruby-sass gulp-cached gulp-uglify gulp-r ...

  6. python datetime获取几分钟、小时、天之前的时间

    import datetime print ((datetime.datetime.now()-datetime.timedelta(days=1)).strftime("%Y-%m-%d ...

  7. VS2010配置QT5.5.0开发环境

    一.官网下载QT和qtvsaddin插件 网址:http://www.qt.io/download-open-source/ 1. 2. 3. 得到下载的安装包,点击安装就能够了 watermark/ ...

  8. multiTarget within one project pods manage

    step1:file->new->target create 1 targetstep2:change Podfile and update podstep3:check new targ ...

  9. 九度OJ 1142:Biorhythms(生理周期) (中国剩余定理)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:266 解决:189 题目描述: Some people believe that there are three cycles in a p ...

  10. ubuntu 16.04安装Jenkins

    快速安装: sudo wget -q -O - http://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - sudo ...