static question

Hello everyone ,i have two question
1)Is there any difference between the declarations static int a and
int static a;

2)
1
2
3
4
5
6
7
8
9
10
11
class one
{
	public:
		int i;
		static int a;
		one(int){a=2;}
		//one(double):a(3){} //wrong
		

	private:
};


The first assignment of static variable is good the second is wrong,why is that since the constructors they do the same thing?Thanks
1) No difference.

2) The ctor-initialization list if for initialising the objects member variables and super-types. Static members are not part of the object. They are part of the class. So initialising them in the object's ctor-initializer doesn't make much sense.
Last edited on
You should initialize your static members (if they are not constant) in your .cpp file.

i.e.

.hpp
1
2
3
4
5
class A
{
  static const int a = 10; //OK since it's constant
  static int b; //error if static int b = 4;
};


.cpp
int A::b = 4;
ok thanks guys :D
Topic archived. No new replies allowed.