Static and non-static members (functions)
C + + Primer "4th edition, page 399:
access to a global object is sometimes necessary for all objects of a particular class type. However, global variables can break encapsulation: an object needs to support an implementation of a particular class abstraction. If the object is global, the normal user code can modify the value.
because of this, a class can define static members of a class, rather than defining a universally accessible global object.
Typically, non-static data members exist in each object of the class type. However,static data members exist independently of any object of the class ;
each static data member is an object associated with a class, not associated with an object of that class .
class can also define static member functions. The static member function does not have the This parameter, it can directly access the static member of the owning class , but cannot use the non-static member directly.
Note: A non-static member function of a class is a static and non-static member of a class that can be accessed directly without a scope operator.
advantages of using static members:
(1) Avoid naming conflicts: the name of the static member is in the scope of the class, so you can avoid conflicts with other classes ' members or global object names.
(2) encapsulation can be implemented: a static member can be a private member, and a global object may not.
(3) Legibility: Static members are associated with a particular class and can show the programmer's intentions.
static members and non-static members call methods:
non-static members are called through the object.
static member through scope operator (direct invocation), object, reference, pointer to object of class type (indirect call)
class lunais{
static double Zty ();
double zzz;
};
Lunais Z;
Lunais *t = &z;
double zty;
zty = Lunais::zty (); //
static member via scope operator (directly called)
zty = Z.zty (); static member via object (simple call)
zty = T->zty (); The static member passes a pointer to the object of the class type (simply called)
static data member definitions:
1, in general, static data members are declared within the class, outside the class definition;
2, static members are not initialized by the class constructor, but are initialized at the time of definition;
3, one exception: initialization is a constant expression, integral type
Static
Const
data member (static
Const
int) can be initialized in the definition body of a class:
class lunais{
static const int zty =;
}
It is important to note that when a const static data member is initialized in the definition body of a class, the data member must still be defined outside the definition of the class, except that the initial value is no longer specified:
const int Lunais::zty;
A constant static const data member cannot be initialized within a class. A good solution is to use a macro definition: #define ZTY 5421
. 5421
###########################################
The following quotations: http://blog.csdn.net/ljfeng123/article/details/20855515
###########################################
Constant-integer static data members can be initialized directly in a class, while a constant-static data member cannot be
Class Circle{int A; //Common Variables, static int b cannot be initialized in the class; //Static Variables, the static const int c=2 cannot be initialized in the class; //static constant Integer variable, you can initialize the static const double Pi=3.1416;//error C2864://Only static constant integer data members can be initialized in the class};
const int CICLE::C; when a const static data member is initialized in the definition body of a class, the data member must still be defined outside the definition of the class, but the initial value is no longer specified B can be initialized outside the class, and all objects share a value of b: int circle::b = 2;
Double circle::P i = 3.1416;
C++_static and non-static members (functions)