C++ Class template Using Static Members

Description

C++ Class template Using Static Members

#include <iostream> 

using namespace std; 

template <typename T> 
class MyClass /*from  www .  j  av  a 2s .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; 
}



PreviousNext

Related