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 ...
随机推荐
- [LeetCode&Python] Problem 884. Uncommon Words from Two Sentences
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- [Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}
def permutationN(n): a=[None]*n for i in range(n): a[i]=i+1 sum=1 for j in range(n): sum*=(j+1) i=0 ...
- [Perl] 删除数组中重复元素
写一个小程序时候,需要去除一个数组中的重复元素,搜索了一下,找到的代码主要是两种,一种是使用grep函数,一种是转换为hash表,代码分别如下: 使用grep函数代码片段:代码: my @array ...
- knowledge 开源知识管理系统
knowledge 是一个不错的知识管理系统,基于markdown 我们可以方便的进行知识的标签 以及展示 使用docker-compose 运行 环境准备 docker-compose 文件 ver ...
- 通过直接编码添加折线图到ChartControl
https://documentation.devexpress.com/#WindowsForms/CustomDocument2976 ChartControl lineChart = new C ...
- tomcat源码阅读之载入器(Loader)
一.Java类的载入器: 双亲委派模型: 1.JVM提供了三种类型的类加载器:引导类载入器(bootstrap class loader).扩展类载入器(extension class loader) ...
- vue-cli、webpack提取第三方库-----DllPlugin、DllReferencePlugin
需要安装的插件有 extract-text-webpack-plugin assets-webpack-plugin clean-webpack-plugin npm install extract- ...
- numpy之meshgrid和where
meshgrid np.meshgrid() np.meshgrid从坐标向量返回坐标矩阵. 这样说可能很抽象.举个例子. x = np.arange(-2,2) y = np.arange(0,3) ...
- MySQL5.5.19安装
本文详细介绍了Windows下安装MySQL5.5.19的全过程,希望对初学者有帮助. http://dl.pconline.com.cn/html_2/1/79/id=465&pn=0.h ...
- DokuWiki
DokuWiki 1.点我,点我,dokuwiki使用教程一 2.点我,点我,dokuwiki使用教程二 3.点我,点我,dokuWiki语法 4.点我,点我,WARP插件