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 ...
随机推荐
- Redis6.2发布 地理位置功能增强了什么?
原文地址:https://developer.aliyun.com/article/780257 Redis社区最近刚刚发布Redis6.2 RC1版本,在本次发布中,阿里云Tair团队(阿里云云内存 ...
- 『学了就忘』Linux基础命令 — 36、查看系统痕迹相关命令
目录 1.w命令 2.who命令 3.last命令 4.lastlog命令 5.lastb命令 系统中有一些重要的痕迹日志文件,如/var/log/wtmp./var/run/utmp./var/lo ...
- celery config
/* Useful celery config. app = Celery('tasks', broker='redis://localhost:6379', backend='redis://loc ...
- 【不费脑筋系列】发布个人的代码包到Nuget服务器上,并通过VS引用进行使用的方法
打打酱油,写点不需要费脑筋的博客先压压惊. 下面讲个关于个人如何开发nuget包,并部署到nuget服务器上的例子.为了保证.net framework和 .net core都可以访问到我的包,我 ...
- Spring Boot程序中@JsonIgnoreProperties与@JsonIgnore基本使用
问题由来: springboot项目中定义了很多类,我们在rest返回中直接返回或者在返回对象中使用这些类,spring已经使用jackson自动帮我们完成这些的to json.但是有时候自动转的js ...
- 执行对象Statement、PreparedStatement和CallableStatement详解
执行对象是SQL的执行者,SQL是"安排好的任务",执行对象就是"实际工作的人". 执行对象有三种: Statement.PreparedStatement和C ...
- C#生成新浪微博短网址 示例源码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DotN ...
- FZU ICPC 2020 寒假训练 1
B - Sum Problem In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input The i ...
- 面向政务企业的开发者工具集-逐浪文本大师v0.1正式发布(含代码全部开源啦!)
这是一款基于.net 4.7环境开发的开发者工具. 一个实用的windows小工具集合,里面包含了多个常用的小软件.其中的批量修改文件名及文件内容功能,可以自定义修改规则,支持规则的导入与导出.不需要 ...
- Python 全局变量和局部变量,global 和 nonlocal关键字
全局变量和局部变量 全局变量:定义在函数外的变量 局部变量:定义在函数内部变量 获取变量值时候先获取当前作用域变量名称和变量值,如果没找到到上一层作用域招变量的值,在没有就报错,先获 ...