Using Static Members in a Template - C++ template

C++ examples for template:template function

Description

Using Static Members in a Template

Demo Code

#include <iostream> 

using namespace std; 

template <typename T> 
class MyClass //from w  w  w .j  a  v a2  s  . c om
{ 
public: 
    static T charge; 
}; 

template <typename T> 
T MyClass<T>::charge; 

int main() 
{ 
    MyClass<int>::charge = 10; 
    MyClass<float>::charge = 98.6; 
    MyClass<int> inst; 
    inst.charge = 22; 

    cout << MyClass<int>::charge << endl; 
    cout << MyClass<float>::charge << endl; 
    cout << inst.charge << endl; 

    return 0; 
}

Result


Related Tutorials