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 ...
随机推荐
- rabbitmq学习(五):springboot整合rabbitmq
一.Springboot对rabbitmq的支持 springboot提供了对rabbitmq的支持,并且大大简化了rabbitmq的相关配置.在springboot中,框架帮我们将不同的交换机划分出 ...
- 在VSCode中配置Eslint格式化
在VSCode中配置Eslint 格式化时使代码保持Eslint语法规范 安装Eslint以及prettier美化插件 在VSCode配置设置项中添加如下代码 { "workbench.co ...
- Hadoop storm大数据分析 知识体系结构
最近工作工作有用到hadoop 和storm,最近看到一个网站上例句的hadoop 和storm的知识体系.所以列出来供大家了解和学习.来自哪个网站就不写了以免以为我做广告额. 目录结构知识点还是挺全 ...
- 1920*1080分辨率和1080p,1080i的关系
1080i和1080p是由美国电影电视工程师协会确定的高清标准格式,其中1080p被称为目前数字电视的顶级显示格式,这种格式的电视在逐行扫描下能够达到1920×1080的分辨率.受限制于存储介质(一部 ...
- MySQL--限制用户使用资源
在MySQL 5.7及后续版本中,可以按照账号来限制每个账号实际具有的资源限制. 语法: GRANT WITH option, 如: GRANT SELECT ON test.* TO user1@l ...
- MSG命令使用详解
最近在编写FTP上传数据的批处理时,需要用到局域网内传输数据来提示错误,突然想起忘了N久没用的命令(net send), 结果在win7 下cmd运行net send /? 运行失败.经过百度大神的 ...
- ORA-32004 的错误处理
启动数据库时,收到了ORA-32004 的错误,错误多是一些过时且在当前版本中不在使用的参数,如果碰到类似的错误,只需要将其 reset即可. SQL> startup;ORA-32004: o ...
- requestAnimationFrame 提高动画性能的原因
与setTimeout相比,requestAnimationFrame最大的优势是由系统来决定回调函数的执行时机.具体一点讲,如果屏幕刷新率是60Hz,那么回调函数就每16.7ms被执行一次,如果刷新 ...
- linq to sql 项目移植后,数据库实体类需要重新创建?
项目中,使用LINQ to SQL 访问数据库,代码移植到其他机器上,每次需要重新生成dbml文件,有无方法只要更改app.config呢? 经过试验是可行的: 1.引用system.configur ...
- bzoj 4650(洛谷 1117) [Noi2016]优秀的拆分——枚举长度的关键点+后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4650 https://www.luogu.org/problemnew/show/P1117 ...