Templates and Static variables in C++
2013-11-26 22:17
513 查看
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.
#include <iostream> using namespace std; template <typename T> void fun(const T& x) { static int i = 10; cout << ++i; return; } int main() { fun<int>(1); // prints 11 cout << endl; fun<int>(2); // prints 12 cout << endl; fun<double>(1.1); // prints 11 cout << endl; getchar(); return 0; }
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.
#include <iostream> using namespace std; template <class T> class Test { private: T val; public: static int count; Test() { count++; } // some other stuff in class }; template<class T> int Test<T>::count = 0; int main() { Test<int> a; // value of count for Test<int> is 1 now Test<int> b; // value of count for Test<int> is 2 now Test<double> c; // value of count for Test<double> is 1 now cout << Test<int>::count << endl; // prints 2 cout << Test<double>::count << endl; //prints 1 getchar(); return 0; }
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
相关文章推荐
- what is the difference between static and normal variables in c++
- static in Java and C++
- [Training Video - 3] [Groovy in Detail] Non-static and Static variables, objects and object referances
- Static member variables in C++
- [CSSinJS] Convert Sass (SCSS) Styled Button to CSSinJS with JavaScript Templates and Variables
- Relevance Between Variable Declaration and Definition in C++
- 《Data Structure And Algorithm Analysis In C++》读书笔记三
- c++ what happens when a constructor throws an exception and leaves the object in an inconsistent state?
- Leetcode_c++:Construct Binary Tree from Inorder and Postorder Traversal (106)
- c++ about SLL(Static-Link Library) and DLL(Dynamic-Link Library)
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++
- static field and method in java
- RAII, Dynamic Objects, and Factories in C++
- Difference between 'struct' and 'typedef struct' in C++?
- Part 35 AngularJS caseInsensitiveMatch and Inline Templates
- [C++]LeetCode: 38 Construct Binary Tree from Inorder and Postorder Traversal
- How to separate the implementation and definition for template function in c++
- Can we use function on left side of an expression in C and C++?
- [Python] How to pack and unpack variables in Python?
- C++详解Leetcode:105. Construct Binary Tree from Preorder and Inorder Traversal