static and non-static members (functions)

static and non-static members (functions)

 " C ++ Primer" 4th Edition 399:
For all objects of a particular class type, access to a global variable is sometimes necessary. However, global variables will destroy the package: the object needs to support the specific implementation of abstract class. If the object is global, the general user code can modify this value.
In view of this, a class can define a class static member, rather than defining a universally accessible global object.
 
Typically, non-static data member is present for each type of object class. However, static data members of the class of the object independently of any exist ;
Each static data member is an object associated with the class , rather than the object of this class is associated .
 
Classes can also define the static member function. static member functions without this parameter , it can be accessed directly static members of its class , but can not directly use a non-static member.
Note: The class of non-static member function is static and non-static class members direct access, without the scope operator.
 
Use static member advantages:
(1) avoid naming conflicts: the name of the static member of the class action in the domain, thus avoiding name conflicts with other members of the class or global object.
(2) the package may be implemented: static private member may be a member, and not the global object.
(3) Readability: static member is associated with a particular class, the programmer's intention can be displayed.
 
static and non-static method call to the members of the members:
Non-static member invoked through an object.
static members by the scope operator (direct call), object reference pointer points to an object of type class (indirect call)
class Lunais{
 
static double zty();
double zzz;
 
};
Lunais with;
Lunais *t = &z;
double zty;
 
:: = Lunais ZTY ZTY (); // static member by the scope operator (direct call)
= z.zty ZTY (); // static member through an object (be indirect call)
= T-ZTY> ZTY (); // static member by pointing a pointer to an object of type class (be indirect call)
 
static data member definitions:
1, under normal circumstances, static data members within a class declaration, defined outside the class;
2, static class member is not initialized by the constructor, but are initialized when defined above;
3, one exception: constant expression initializer, integer static  const  data members ( static  const  int ) can be initialized in the class definition of the body:
 
class Lunais{
 
static const int zty = 30;
 
}
It is noted that: when const static data members of the class definition-initialized, the data must still be a member of the class definition in the definition vitro, but no longer specifies the initial value:
const int Lunais::zty;
 
Real normally static const data members within the class is not initialized. A good solution is to use a macro definition: #define ZTY 5421 .5421

 

###########################################

The following quote from: http://blog.csdn.net/ljfeng123/article/details/20855515

###########################################

Often integer static data members may be directly in the class initialization, and often not real static data members

 class circle
{
int a; // common variable , can not be initialized in the class
static int b; // static variables , initialization is not in the class
static const int c = 2; // static constant integer variable may be initialized in the class
static const double PI = 3.1416; // error C2864: // only static const integral data members can initialize in the class
} ;
 
const int cicle :: c; // time const static data members of the class definition-initialized, the data must still be a member of the class definition in the definition vitro, but no longer specifies the initial value
 
b may be initialized outside the class, and all objects share a value of b:
int circle::b = 2;
 
double circle::PI = 3.1416;
Category: C ++

Guess you like

Origin blog.csdn.net/weixin_42187898/article/details/93625986