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. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 template <typename T> void fun(const T& x)
  5. 5 {
  6. 6 static int i = 10;
  7. 7 cout << ++i;
  8. 8 return;
  9. 9 }
  10. 10
  11. 11 int main()
  12. 12 {
  13. 13 fun<int>(1); // prints 11
  14. 14 cout << endl;
  15. 15 fun<int>(2); // prints 12
  16. 16 cout << endl;
  17. 17 fun<double>(1.1); // prints 11
  18. 18 cout << endl;
  19. 19 getchar();
  20. 20 return 0;
  21. 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. 1 #include <iostream>
  2. 2
  3. 3 using namespace std;
  4. 4
  5. 5 template <class T> class Test
  6. 6 {
  7. 7 private:
  8. 8 T val;
  9. 9 public:
  10. 10 static int count;
  11. 11 Test()
  12. 12 {
  13. 13 count++;
  14. 14 }
  15. 15 // some other stuff in class
  16. 16 };
  17. 17
  18. 18 template<class T> int Test<T>::count = 0;
  19. 19
  20. 20 int main()
  21. 21 {
  22. 22 Test<int> a; // value of count for Test<int> is 1 now
  23. 23 Test<int> b; // value of count for Test<int> is 2 now
  24. 24 Test<double> c; // value of count for Test<double> is 1 now
  25. 25 cout << Test<int>::count << endl; // prints 2
  26. 26 cout << Test<double>::count << endl; //prints 1
  27. 27
  28. 28 getchar();
  29. 29 return 0;
  30. 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. 一条指令优化引发的血案,性能狂掉50%,clang使用-ffast-math选项后变傻了

    https://www.cnblogs.com/bbqzsl/p/15510377.html 近期在做优化时,对一些函数分别在不同编译平台上进行bench测试.发现了不少问题. 现在拿其中一个问题来分 ...

  2. ICMP 协议仿真及ping命令用途

    1.实验目的 加深对 IPv4 协议首部各定义域的理解,掌握路由表的结构和基本配置命令,熟悉 ICMP 的调试操作. 2.实验原理 IPv4 协议定义,网络层协议的相关 RFC 定义和描述. 3.实验 ...

  3. 有关unsigned和有符号类型的区别

    相信大家对于unsigned这个玩意并不陌生,但是有的时候却会被它搞懵,比如下面: #include<iostream> using std::cout; using std::cin; ...

  4. IDEA插件和个性化配置推荐

    插件推荐 我自己现在使用的一些插件和一些自己感觉比较舒服配置分析给大家 idea如何安装插件: 如果打开设置没有看到,直接搜索plugins 然后在这里搜索即可 CodeGlance 小地图 和vsc ...

  5. 一维前缀和 连续数组和为k

    给定一个整数数组和一个整数 k ,请找到该数组中和为 k 的连续子数组的个数. 滑动窗口没办法解决有负数的情况 方法一: 预处理 前缀和 sum_ij = preSum[j] - preSum[i-1 ...

  6. 使用BadBoy录制JMeter脚本

     BadBoy简介 BadBoy是一款免费WEB自动化测试工具,其实就是一个浏览器模拟工具,具有录制和回放功能,支持对录制出来的脚本进行调试.同时支持捕获表单数据的功能,所以能够进行自动化测试.但目前 ...

  7. 事件消息生产消费中间件-OSS.DataFlow

    系统重构解耦的过程涉及不同领域服务分拆,或同一服务下实时响应部分和非响应部分分拆,分解后的各部分通过异步消息的流转传递,完成整体的业务逻辑,但是频繁的在业务层面直接调用不同消息队列的SDK,个人感觉不 ...

  8. 痞子衡嵌入式:在IAR开发环境下RT-Thread工程函数重定向失效分析

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是在IAR开发环境下RT-Thread工程函数重定向失效分析. 痞子衡旧文 <在IAR下将关键函数重定向到RAM中执行的方法> ...

  9. 网络带宽和速度测试windows和linux用iperf工具

    网络带宽和速度测试windows和linux用iperf工具   Iperf是一个网络性能测试工具.Iperf可以测试TCP和UDP带宽质量.Iperf可以测量最大TCP带宽,具有多种参数和UDP特性 ...

  10. <C#任务导引教程>练习六

    //54五名学生参加了两门课程的考试,试输入成绩,统计出各科的最高分,最低分,平均分及每个学生的平均成绩(要求用对话框显示成绩统计结果)using System;using System.Window ...