I am struggling with the static
keyword in CPP.
After my experiments I found:
static
member is not inside the class. I calculated the size of the class before and after I add a static member, I found they are the same. One explanation that occurred to me is, perhaps the class name become the namespace (scope of this static member)? and that member is outside the class?- Function is not inside the class, the have a implicit argument like the object pointer. however why the size of the class will be one but not 0 if there is no field inside the class but have a bunch of function? but is the pointer also give the function a scope that allow the function to access to the private member or the function its self have the class namespace.
- We could use both class name and object to access the static field and function, why we could do that. if the class name is the name space to the static function, why we could use an object to access it? is the object itself have the class namespace?
- Then i try to declare and define a static member inside class, and of course it fail, but i want to know why? why we could not initial it inside class?
XD English is not my mother language, sorry for my grammar mistake XD.