c++官方文档-模版类
#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++官方文档-模版类的更多相关文章
- Guava官方文档-RateLimiter类
转载自并发编程网 – ifeve.com RateLimiter 从概念上来讲,速率限制器会在可配置的速率下分配许可证.如果必要的话,每个acquire() 会阻塞当前线程直到许可证可用后获取该许可证 ...
- c++官方文档-模版函数和重载
#include<stdio.h> #include<iostream> #include<queue> #include<map> #include& ...
- python附录-builtins.py模块str类源码(含str官方文档链接)
python附录-builtins.py模块str类源码 str官方文档链接:https://docs.python.org/3/library/stdtypes.html#text-sequence ...
- Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)
Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...
- Django 2.0官方文档中文 总索引
Django 2.0官方文档中文 渣翻 总索引 翻译 2017年12月08日 11:19:1 官方原文: https://docs.djangoproject.com/en/2.0/ 当前翻译版本: ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(中)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(下)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- Kotlin开发语言文档(官方文档)-- 目录
开始阅读Kotlin官方文档.先上文档目录.有些内容还未阅读,有些目录标目翻译还需琢磨琢磨.后续再将具体内容的链接逐步加上. 文档链接:https://kotlinlang.org/docs/kotl ...
随机推荐
- PAT 团体程序设计天梯赛 L1-046 整除光棍(模拟除法)
L1-046. 整除光棍 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 翁恺 这里所谓的"光棍",并不是指单身汪 ...
- Windows10 解决“装了 .NET Framework 4.5.2/4.6.1/4.7.1等等任何版本 或版本更高的更新”问题
========================================================= 系统自带的.net framework版本为4.7,自己安装.NET Framewo ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
- sql server 创建内联表值函数
表值函数就是返回table 的函数使用它可以方便的进行查询的处理 创建的代码如下: create FUNCTION returunclassfirstlist( -- Add the paramet ...
- 两数之和 Two Sum
给定一个整数数列,找出其中和为特定值的那两个数. 你可以假设每个输入都只会有一种答案,同样的元素不能被重用. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 n ...
- Spring Cloud 入门 之 Hystrix 篇(四)
原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...
- c# ProgressBar进度条方向和美观
protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.Style | ...
- Maven 项目报告插件
Maven 项目报告插件,都是对于前面生成的项目站点的内容丰富,因此都是基于项目站点的,生成的命令和生成项目站点一致(mvn site),项目报告插件的配置和一般插件不同,是在 project-> ...
- Android 禁止系统进入深度休眠
在Linux系统中,wake_lock是一直锁机制,只要有驱动占用这个锁,系统就不会进入深度休眠. 获取此锁的方法有两种: 1.在adb中通过指令获取wake_lock,系统就不会进入深度休眠 ech ...
- Windows(x64)编译FFMPEG-2.0.1
一.引言 公司需要做网络视频传输的相关项目,初步选定用这么几个东西FFMPEG,ORTP,Live555这么几个东东.研究了也有一个月了,把一些心得写出来,这篇文章主要介绍FFMPEG在windows ...