#include <iostream>
using namespace std; template<class T>
class MyPair
{
private:
T t[];
T a, b;
public:
MyPair(T t1, T t2)
: a(t1), b(t2)
{
t[] = t1;
t[] = t2;
}
const T* getT()
{
return t;
}
T getMax();
};
/**
* 第一个T指定模版参数
* 第二个T指定函数返回的类型
* 第三个T指定函数的模版参数是类的模版参数
*
*/
template<class T> T MyPair<T>::getMax()
{
//不知道retval的具体类型,所以返回一个临时变量
T retval;
retval = a > b ? a : b;
return retval;
}
template<class R> R f(const R& t)
{
R r = t;
//错误
// t= r;
return r;
} //为模版定义不同的实现
template<class T>
class mycontainer
{
T element;
public:
mycontainer(T args)
{
this->element = args;
}
T increase()
{
return ++element;
}
}; //class template specialization
//模版实例化
//This is because all types
//are known and no template arguments are required for this specialization
//这个是因为所有类型都已经知道而且实例化并不需要模版参数
template<>
class mycontainer<char>
{
char element;
public:
mycontainer(char arg)
{
element = arg;
}
char uppercase()
{
if((element >= 'a') && (element <= 'z'))
element += 'A' - 'a';
return element;
}
};
//template <class T> class mycontainer { ... };//The first line is the generic template
//template <> class mycontainer <char> { ... };//and the second one is the specialization int main()
{
//注释这句话会导致上面那个函数没有编译,能编译通过
f<int>();
MyPair<int> myint(, );
//非常量指针指向常量空间
const int* t = myint.getT();
// t[0]=2000;//invalid
cout << t[] << endl;
cout << myint.getT()[] << endl;
int tt[] = { , };
t = tt;
cout << t[] << endl;
cout << myint.getT()[] << endl;
MyPair<double> mydouble(3.0, 4.0);
cout << mydouble.getMax() << endl; mycontainer<int> myintcontainer();
mycontainer<char> mychar('j');
cout << myintcontainer.increase() << endl;
cout << mychar.uppercase() << endl;
return ;
}

模版函数和模版类

#include <iostream>
using namespace std; class Example4
{
string* ptr;
public:
// constructors:
Example4()
: ptr(new string)
{
}
Example4(const string& str)
: ptr(new string(str))
{
}
// destructor:
~Example4()
{
cout << "调用析构函数" << endl;
delete ptr;
}
// access content:
const string& content() const
{
return *ptr;
}
}; template<class T>
class myClass
{
T t;
public:
myClass(T t)
: t(t)
{ }
template<class R> T f(R const& r, T const& t);
};
template<class T> template<class R> T myClass<T>::f(R const& r, T const& t)
{
cout << "r=" << r << endl;
return t;
} int main()
{
//The destructor for an object is called at the end of its lifetime;
//in the case of foo and bar this happens at the end of function main.
//析构函数在对象生命周期结束后被调用,在这个列子中,foo和bar在main函数的结束时结束
//可以理解成从栈中弹出么
Example4 foo;
Example4 bar("Example");
Example4* fp = new Example4("Example pointer");
cout << "bar's content: " << bar.content() << '\n';
cout << "fp's content: " << fp->content() << '\n';
//指针要手动调用,new 出来的对象,内存块在堆上,不在当前函数栈里
//可以把下面这句话注释了看下
delete fp; myClass<int> myint();
int i = myint.f<double>(200.01, );
cout << i << endl;
return ;
}

下面这个模式就有点奇怪了

#include <iostream>
using namespace std; class Example4
{
string* ptr;
public:
// constructors:
Example4()
: ptr(new string)
{
}
Example4(const string& str)
: ptr(new string(str))
{
}
// destructor:
~Example4()
{
cout << "调用析构函数" << endl;
delete ptr;
}
// access content:
const string& content() const
{
return *ptr;
}
}; template<class T>
class myClass
{
T t;
public:
myClass(T t)
: t(t)
{ }
template<class R> R f(R const& r, R const& t);
};
template<class T> template<class R> R myClass<T>::f(R const& r, R const& t)
{
cout << "r=" << r << endl;
return t;
} int main()
{
//The destructor for an object is called at the end of its lifetime;
//in the case of foo and bar this happens at the end of function main.
//析构函数在对象生命周期结束后被调用,在这个列子中,foo和bar在main函数的结束时结束
//可以理解成从栈中弹出么
Example4 foo;
Example4 bar("Example");
Example4* fp = new Example4("Example pointer");
cout << "bar's content: " << bar.content() << '\n';
cout << "fp's content: " << fp->content() << '\n';
//指针要手动调用,new 出来的对象,内存块在堆上,不在当前函数栈里
//可以把下面这句话注释了看下
delete fp; myClass<int> myint();
cout << myint.f<double>(100.0, 100.0)<<endl;
return ;
}

c++官方文档-模版类的更多相关文章

  1. Guava官方文档-RateLimiter类

    转载自并发编程网 – ifeve.com RateLimiter 从概念上来讲,速率限制器会在可配置的速率下分配许可证.如果必要的话,每个acquire() 会阻塞当前线程直到许可证可用后获取该许可证 ...

  2. c++官方文档-模版函数和重载

    #include<stdio.h> #include<iostream> #include<queue> #include<map> #include& ...

  3. python附录-builtins.py模块str类源码(含str官方文档链接)

    python附录-builtins.py模块str类源码 str官方文档链接:https://docs.python.org/3/library/stdtypes.html#text-sequence ...

  4. Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)

    Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...

  5. Django 2.0官方文档中文 总索引

    Django 2.0官方文档中文 渣翻 总索引 翻译 2017年12月08日 11:19:1 官方原文: https://docs.djangoproject.com/en/2.0/ 当前翻译版本:  ...

  6. 【AutoMapper官方文档】DTO与Domin Model相互转换(上)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  7. 【AutoMapper官方文档】DTO与Domin Model相互转换(中)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  8. 【AutoMapper官方文档】DTO与Domin Model相互转换(下)

    写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...

  9. Kotlin开发语言文档(官方文档)-- 目录

    开始阅读Kotlin官方文档.先上文档目录.有些内容还未阅读,有些目录标目翻译还需琢磨琢磨.后续再将具体内容的链接逐步加上. 文档链接:https://kotlinlang.org/docs/kotl ...

随机推荐

  1. Android Studio安装、配置教程全 - 安卓开发环境的配置手册

    Android Studio的安装.配置 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 一 Downloa ...

  2. 20155316 2016-2017-2 《Java程序设计》第5周学习总结

    教材学习内容总结 这周总结 try catch语法 异常继承结构 throw finally AutoCloseable接口 Collection Map Lambda表达式 上周总结 三个关键 类与 ...

  3. (3)re模块(正则表达式模块)

    什么是正则表达式 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 r ...

  4. 网络流--最大流ek模板

    标准大白书式模板,代码简单但由于效率并不高,所以并不常用,就是这样 #include<stdio.h> #include<string.h> #include<queue ...

  5. [转]G++与GCC的区别

    转自http://www.52pojie.cn/thread-58109-1-1.html 误区一:gcc只能编译c代码,g++只能编译c++代码两者都可以,但是请注意:1.后缀为.c的,gcc把它当 ...

  6. Hadoop全分布模式操作

    http://blog.csdn.net/wangloveall/article/details/20767161 摘要:介绍Hadoop全分布模式操作,实现真正意义上的集群架构. 关键词:Hadoo ...

  7. scanner 在java中的输入

    ************************************************************************************** 和C++的输入很像

  8. PHP独立环境搭建细节

    一.安装前准备: 准备安装软件此处以以下软件为例: Appache:httpd-2.2.21-win32-x86-openssl-0.9.8r.msi MySQL: mysql-5.5.21-win ...

  9. DDD领域驱动设计 ---- 系列文章

    C#进阶系列——DDD领域驱动设计初探(七):Web层的搭建 C#进阶系列——DDD领域驱动设计初探(六):领域服务 C#进阶系列——DDD领域驱动设计初探(五):AutoMapper使用 C#进阶系 ...

  10. ASP.NET MVC中如何实现页面跳转

    1,最简单的方式:超链接 以下分别是连接到HomeController控制器下的SharpL动作方法,以及百度首页.代码如下: <a href="Home\SharpL"&g ...