Static properties of c ++ class member

1, we can use the  static  keyword to the class members is defined as static. When we declare a member of the class is static, which means that the object is created, regardless of how many classes, static members have only one copy.

2, static members in all classes of objects are shared. If other initialization statement does not exist, when you create the first object, all static data will be initialized to zero. We can not initialize static member is placed in the class definition, but can be resolved by the operator outside the class range  ::  re-claim initialize it so that the static variable.

3, if the function member declared as static, you can open an independent function with any particular object class. Static member function even if the class object does not exist can also be called a static function as long as the class name plus the scope resolution operator  ::  to access.

4, static member functions can only access static data members, other static member function and other functions outside the class.

5, there is a class static member function scope, they can not access this pointer class. You can use a static member function to determine whether a certain object classes have been created.

6, static member functions and member functions common difference:

  • This is not a static member function pointers can only access static members (including static member variables and static member functions).
  • Ordinary members have this function pointer, you can access any member of the class; but this is not a static member function pointers.

ps:

Special class member variable initialization problem:

  • Constant variable: must be initialized by the constructor parameter list.
  • Reference variable: must be initialized by the constructor parameter list.
  • Ordinary static variables: to be out of class by "::" initialization.
  • Static int constants: can be initialized directly when defined.
  • Static non-integer constant: can not be initialized directly when defined. To outside of class by "::" Initialization

 

Guess you like

Origin www.cnblogs.com/cqu-qxl/p/11458669.html