Original Post
Question:
Why can I put static template class variable declaration to header file, but if I put non-template class static variable declaration there it gives ''multiple definition of...'' error?
Sample code:
Now I include this header from test1.cpp and test2.cpp.
I get link-time error: multiple definition of `TestNoTemplate::intvalue''
I know I have to put the declaration to source-file, not to header file.
But how does templates handle this automatically?
That''s no problem to me, I''m just interested knowing
//
// This code in tnttest.h
//
//
// Non-template class with static variable
//
class TestNoTemplate
{
public:
static int intvalue;
};
// Static variable declaration
int TestNoTemplate::intvalue = 0;
//
// Template class with static variable
//
template<class T> class TestTemplate
{
public:
static T Tvalue;
};
// Templated static variable declaration
template<class T> T TestTemplate<T>::Tvalue=0;