Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use static variable in a C++ class template

This is an example taken from geeksforgeeks. I don't understand the following code.

template<class T> int Test<T>::count = 0;

Is count an external variable? Why not just let static int count = 0? The description and code in the geeksforgeeks is listed below.

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;
}
like image 302
BVBC Avatar asked Feb 24 '16 05:02

BVBC


People also ask

How can we use static variables in class?

Static variables in a class: As the variables declared as static are initialized only once as they are allocated space in separate static storage so, the static variables in a class are shared by the objects. There can not be multiple copies of same static variables for different objects.

Can a template be static?

A template is referred to as being "static" if no data is inserted into it when it is created or if it is not modified in any other way by Templafy. The opposite of a static template is a dynamic template.

How does a static variable work in C?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

Can we declare static variable in C?

Static is a keyword used in C programming language. It can be used with both variables and functions, i.e., we can declare a static variable and static function as well. An ordinary variable is limited to the scope in which it is defined, while the scope of the static variable is throughout the program.


2 Answers

Every time you instantiate Test object with new type, a new class from available template is created for you. (So in your case, there Test<int> and Test<double> classes created on demand for you by the compiler). You can now think of Test<int> and Test<double> as 2 separate classes created from the same template.

Because there are two classes, there are two copies of static variable with same name in different scopes. template<class T> int Test<T>::count = 0; is a template for the definition of this count in classes created on demand.

If you specialize this definition for some type, for ex:

template<>
int Test<int>::count = 5;

Test<int>::count would be 7 at the time of print it. While Test<double>::count would remain 1 (unchanged).

like image 180
Mohit Jain Avatar answered Sep 28 '22 14:09

Mohit Jain


count is not an external variable. The reason why it is outside the class like that is because the variable needs to be allocated (and maybe instantiated). When a static variable is inside a class definition, it only tells the compiler "there is going to be this kind of variable" but since definitions may be included in many source files the compiler won't do any allocations.

When the compiler sees the external definition it knows to allocate space for it and instantiate it if it is an object. This may happen only once, so it cannot be in a header file.

like image 36
Sami Kuhmonen Avatar answered Sep 28 '22 12:09

Sami Kuhmonen