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++的更多相关文章

  1. Django下的templates 和 static静态文件

    如果Django顶层目录中没有templates的话,就自己新建一个Directory ,这个文件是存放html文件的 1)如果在views里面用render(request,"" ...

  2. django配置templates、static、media和连接mysql数据库

    1.模板文件 # =======templates配置======= if os.path.exists(os.path.join(BASE_DIR, 'templates')) is False: ...

  3. [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() ...

  4. django入门 02 初探app、view、url、templates、static

    创建APP命令 python manage.py startapp myapp app组成介绍 如上图,在终端中展示树状结构-- windows为 tree /f macOS为 tree 注册APP ...

  5. Static variables in JavaScript

    function MyClass () { // constructor function var privateVariable = "foo"; //NO:obj.privat ...

  6. #Java编程思想笔记(一)——static

    Java编程思想笔记(一)--static 看<Java编程思想>已经有一段时间了,一直以来都把笔记做在印象笔记上,今天开始写博客来记录. 第一篇笔记来写static关键字. static ...

  7. Django 静态文件配置(static files)

    Django version: 1.9 Python versrion: 3.5.2 这几天Django配置静态文件(本例是要加载index.css), 总是不对,最后终于试对了,这里记录下,方便以后 ...

  8. C++ essentials 之 static 关键字

    extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...

  9. iOS static

    获得20条news 先实现,再提取到business 层. The static Keyword You can have a local variable retain its value thro ...

随机推荐

  1. 力扣 - 剑指 Offer 06. 从尾到头打印链表.md

    题目 剑指 Offer 06. 从尾到头打印链表 思路1(递归) 首先先遍历整个脸表,计算出链表的长度(用于初始化数组).然后进行递归,从链表头部递归到尾部,这期间什么都不做,直到递归到最后一个节点的 ...

  2. 【java+selenium3】隐式等待+显式等待 (七)

    一.隐式等待 -- implicitlyWait 调用方式:driver.manage().timeouts().implicitlyWait(long time, TimeUnit unit); / ...

  3. let that = this用法解析

    这种情况就是在一个代码片段里this有可能代表不同的对象,而编码者希望this代表最初的对象

  4. mongodb(一)

    文档是 MongoDB 的核心,类似于 SQLite 数据库(关系数据库)中的每一行数据.多个键及其关联的值放在一起就是文档.在 Mongodb 中使用一种类 json 的 bson 存储数据,bso ...

  5. java-TCP协议发送和接收数据

    TCP协议接收数据的步骤: A:创建接收数据的Socket对象 创建对象的时候要指定端口 B:监听客户端连接 等待客户端连接 C:获取Socket对象的输入流(字节流) D:读数据,并显示在控制台 E ...

  6. 【知识详解】JAVA基础(秋招总结)

    JAVA基础 目录 JAVA基础 问:面向过程(POP)和面向对象(OOP)? 问:Python和Java的区别? 问:java的八大基本数据类型? 问:封装继承多态说一下? 问:方法和函数的区别? ...

  7. HCNP Routing&Switching之组播技术-组播地址

    前文我们聊到了组播技术背景,单播.广播在点到多点应用中的问题,以及组播对比单播.广播在点到多点的网络环境中的优势.劣势,相关回顾请参考https://www.cnblogs.com/qiuhom-18 ...

  8. 深入理解Redis 数据结构—简单动态字符串sds

    Redis是用ANSI C语言编写的,它是一个高性能的key-value数据库,它可以作用在数据库.缓存和消息中间件.其中 Redis 键值对中的键都是 string 类型,而键值对中的值也是有 st ...

  9. Jmeter——变量嵌套函数使用(__V)案例分析

    jmeter版本:5.3 __V官方函数解释: (https://jmeter.apache.org/usermanual/functions.html#__V) 图1-1 解决问题:实现字符串拼接 ...

  10. URL URI傻傻分不清楚,dart告诉你该怎么用

    目录 简介 dart中的URI encode和decode 解析URI 总结 简介 如果我们要访问一个网站,需要知道这个网站的地址,网站的地址一般被称为URL,他的全称是Uniform Resourc ...