A static member with an in-class initializer

Feb 9, 2010 at 9:37pm
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 Feb 9, 2010 at 9:38pm
Feb 9, 2010 at 9:40pm
They are functionally equivalent.
Feb 9, 2010 at 9:56pm
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;
};
Feb 10, 2010 at 1:18pm
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.