A static member with an in-class initializer

Hi there!

If we need a static member, we can declare it:

* without an in-class initializer (this requires definition outside the class)

1
2
3
4
5
6
class T
{
	static int s_mem;
};
//...
int T::s_mem = 5;


* with an in-class initializer (in this case, it depends on us whether we define static member outside the class or we don't):

1
2
3
4
5
6
class T
{
	static const int s_mem = 5;
};
//...
const int T::s_mem;	//<---- it's not necessary 


Do these two ways differ - except place of initialization ?

Thank you for your help :)
Last edited on
They are functionally equivalent.
And we can say the same thing about these codes, can't we ?

1
2
3
4
5
6
class T
{
	static const int s_mem = 5;
};
//...
const int T::s_mem;	   //<---- it's not necessary 


1
2
3
4
class T
{
	static const int s_mem = 5;
};
Sorry, in your original post I missed that one declaration was const while the other was not.

So the examples you gave in your reply are functionally equivalent. The examples in your
original post are not, since in the first case s_mem is changeable at runtime whereas in the
second case it is not.
Topic archived. No new replies allowed.