Templates and Static variables in C++
Function templates and static variables:
Each instantiation of function template has its own copy of local static variables.
For example, in the following program there are two instances: void fun(int ) and void fun(double ). So two copies of static variable i exist.
1 #include <iostream>
2 using namespace std;
3
4 template <typename T> void fun(const T& x)
5 {
6 static int i = 10;
7 cout << ++i;
8 return;
9 }
10
11 int main()
12 {
13 fun<int>(1); // prints 11
14 cout << endl;
15 fun<int>(2); // prints 12
16 cout << endl;
17 fun<double>(1.1); // prints 11
18 cout << endl;
19 getchar();
20 return 0;
21 }
Output of the above program is:
11
12
11
Class templates and static variables:
The rule for class templates is same as function templates, Each instantiation of class template has its own copy of member static variables.
For example, in the following program there are two instances Test and Test. So two copies of static variable count exist.
1 #include <iostream>
2
3 using namespace std;
4
5 template <class T> class Test
6 {
7 private:
8 T val;
9 public:
10 static int count;
11 Test()
12 {
13 count++;
14 }
15 // some other stuff in class
16 };
17
18 template<class T> int Test<T>::count = 0;
19
20 int main()
21 {
22 Test<int> a; // value of count for Test<int> is 1 now
23 Test<int> b; // value of count for Test<int> is 2 now
24 Test<double> c; // value of count for Test<double> is 1 now
25 cout << Test<int>::count << endl; // prints 2
26 cout << Test<double>::count << endl; //prints 1
27
28 getchar();
29 return 0;
30 }
Output of the above program is:
2
1
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 22:16:57
Templates and Static variables in C++的更多相关文章
- Django下的templates 和 static静态文件
如果Django顶层目录中没有templates的话,就自己新建一个Directory ,这个文件是存放html文件的 1)如果在views里面用render(request,"" ...
- django配置templates、static、media和连接mysql数据库
1.模板文件 # =======templates配置======= if os.path.exists(os.path.join(BASE_DIR, 'templates')) is False: ...
- [Training Video - 3] [Groovy in Detail] Non-static and Static variables, objects and object referances
log.info "starting" // we use class to create objects of a class Planet p1 = new Planet() ...
- django入门 02 初探app、view、url、templates、static
创建APP命令 python manage.py startapp myapp app组成介绍 如上图,在终端中展示树状结构-- windows为 tree /f macOS为 tree 注册APP ...
- Static variables in JavaScript
function MyClass () { // constructor function var privateVariable = "foo"; //NO:obj.privat ...
- #Java编程思想笔记(一)——static
Java编程思想笔记(一)--static 看<Java编程思想>已经有一段时间了,一直以来都把笔记做在印象笔记上,今天开始写博客来记录. 第一篇笔记来写static关键字. static ...
- Django 静态文件配置(static files)
Django version: 1.9 Python versrion: 3.5.2 这几天Django配置静态文件(本例是要加载index.css), 总是不对,最后终于试对了,这里记录下,方便以后 ...
- C++ essentials 之 static 关键字
extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...
- iOS static
获得20条news 先实现,再提取到business 层. The static Keyword You can have a local variable retain its value thro ...
随机推荐
- css书写规范 & 页面布局规范 &常用案例经验总结
CSS 属性书写顺序(重点) 建议遵循以下顺序: 布局定位属性:display / position / float / clear / visibility / overflow(建议 displa ...
- Python3使用request/urllib库重定向问题
禁止自动重定向 python3的urllib.request模块发http请求的时候,如果服务器响应30x会自动跟随重定向,返回的结果是重定向后的最终结果而不是30x的响应结果. request是靠H ...
- 菜鸟Markdown笔记,看这个就够了
菜鸟markdown语法笔记 1.标题 写法:共六级标题,一个#是一级标题,两个#是二级标题,三个#是三级标题······以此类推 (#)+空格键,快捷方式是Ctrl+1/2/3/4/5/6 2.段落 ...
- 问题 F: 背包问题
题目描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w<=10):如果给你一个背包它能容纳的重量为m(10<=m<=20), ...
- Linux基础二:文件系统
二.文件系统: Linux系统一切皆文件!整个文件系统是一棵颠倒过来的树形结构,根目录/在顶部,且从根目录到下面的任一文件有且仅有一条路径. 1.重要目录: /usr -> 存放普通用户命令(/ ...
- [atAGC045B]01 Unbalanced
将0变为-1后求前缀和,那么$s$的价值即为最大的前缀和-最小的前缀和(特别的,空前缀的前缀和为0) 令$f(x)$表示当最大的前缀和不大于$x$时,最小的前缀和最大是多少,答案即为$\min_{x} ...
- 使用国内的镜像源搭建 kubernetes(k8s)集群
1. 概述 老话说的好:努力学习,提高自己,让自己知道的比别人多,了解的别人多. 言归正传,之前我们聊了 Docker,随着业务的不断扩大,Docker 容器不断增多,物理机也不断增多,此时我们会发现 ...
- 如何在 ShardingSphere 中开发自己的 DistSQL
在<DistSQL:像数据库一样使用 Apache ShardingSphere>和<SCTL 涅槃重生:投入 RAL 的怀抱>中,已经为大家介绍了 DistSQL 的设计初衷 ...
- c语言printf输出最前端字符不显示
原因:语法错误,和其它语言语法混用. printf("链表长度 : %d \n",length); printf("length is : %d \n",len ...
- Codeforces 718E - Matvey's Birthday(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 首先注意到这个图的特殊性:我们对于所有 \(s_i=s_j\) 的 \((i,j)\) 之间都连了条边,而字符集大小顶多只有 \(8\ ...