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 ...
随机推荐
- 直播预告|App 首页如何动态化更新?来看蚂蚁技术专家详解「支付宝」全新卡片技术栈
立即前往直播间预约观看 从icon到card,一场内容前置化的变革 从 Windows 时代开始,应用程序图标就成为了用户(流量)的主入口,一直持续到移动端时代. 图标即入口的方式,虽然足够方便但却不 ...
- .NET 开源工作流: Slickflow流程引擎高级开发(九) -- 条件事件模式解释及应用
前言:在流程流转过程中,有时候需要条件模式的支持,这样可以使得流程流转更加灵活多变.比如在业务变量满足一定的条件时,可以启动特定配置的流程(或者位于主流程内部的子流程).本文主要描述条件启动和条件中间 ...
- 移动GPU分类/百科
ARM mali gpu四大微架构概述 https://zhuanlan.zhihu.com/p/107141045 http://www.neardi.com/news_23/487.html
- HTTP1.1 Keep-Alive到底算不算长连接?
在基础架构部沉浸了半年,有一些认知刷新想和童靴们交代一下, 不一定全面,仅代表此时的认知, 也欢迎筒靴们提出看法. 本文聊一聊口嗨用语:"长连接.短连接", 文章会按照下面的思维导 ...
- php简单手机商品发布系统
原本还说学学angular2的,没想到上一公司呆了两月就走了,现在在这个公司做了一个小型的商品发布系统,,php实现的,比较简单,功能不多,是以手机模板发布商品网站的,需要的可以拿去 http://p ...
- [源码解析] PyTorch 分布式(8) -------- DistributedDataParallel之论文篇
[源码解析] PyTorch 分布式(8) -------- DistributedDataParallel之论文篇 目录 [源码解析] PyTorch 分布式(8) -------- Distrib ...
- Java 如何对文件进行多个Object对象流的读写操作
思路:把已经序列化的对象存入容器(如LinkedList<?>)中,然后用ObjectInputStream和ObjectOutputStream对这个实例化的LinkedList< ...
- [bzoj4003]城市攻占
倍增,对于每一个点计算他走到$2^i$次祖先所需要的攻击力以及最终会变成什么(一个一次函数),简单处理即可(然而这样是错的,因为他只保证了骑士的攻击力可以存,并没有保证这个一次函数的系数可以存)(其实 ...
- Java-ASM框架学习-java概念转字节码概念
前言 当我们操作字节码的时候,都是和字节码的概念打交道,这让我们很困扰,asm也想到了这点,为了方便,它提供了一个可以把java概念转化为字节码概念的类 import org.objectweb.as ...
- 微信小程序中途加入云开发之坑
一开始未使用云开发的小程序项目,之后想使用云开发能力时,要先删除对应在开发者工具中的项目(先压缩备份源码!),再用开发者工具重新创建,很多时候都需要用这种方式进行处理