The third job static C ++

c ++ is static


static data sharing for C ++ has a very important role.

Declared in a class static data is subordinate to the class rather than an object, which means that all of the objects have common data:

#include<iostream>
using namespace std;
 
class Point
{
public:
    Point(int xx = 0, int yy = 0) { X = xx; Y = yy; countP ++; }
    Point(Point &p); 
    ~Point() { countP --; }
    int GetX() { return X; }
    int GetY() { return Y; }
    void GetC() { cout << "Object id = " << countP << endl; } //输出静态数据成员
private:
     Int X-, the Y;
     static  int countP; // static data member declaration, for recording the number of points 
}; 
 
Point :: Point (Point & P) 
{ 
    X- = of pX; 
    the Y = pY &; 
    countP ++; // of countP accumulated in the constructor, all objects with a safeguard countP 
} 
 
int Point :: countP = 0 ; // static data member definition and initialization, the class name is defined 
 
int main () 
{ 
    Point a ( . 4 , . 5 ); // definition object A, which would countP by the constructor. 1 
    A.GetC (); // number of output object
    B Point (A); // define the object B, which would countP by the constructor. 1 
    B.GetC (); 
 
    return  0 ; 
}

 

 

 

to sum up:

For non-static data members, each object has its own copy of the class. The static data member is treated as a member of the class. Whether objects of this class is defined as the number of static data members and only one copy of the program, the share accessible to all objects of that type. In other words, static data members are common to all objects of that class. A plurality of objects of this class, the static data allocated only one memory member, common to all objects. Therefore, the value of the static data member is the same for each object,

 

 

 Note: initialize static class members on the outside, because the static members are not part of the object before the object has not been instantiated by the object is not to visit, but it can be directly accessed through the class name to the outside

Because static members do not need to instantiate the class had been allocated memory space.

 

Static member function:

Modified static over the static member function and static data like, are not covered, but belong to the class, have to pay attention to is a static member functions can only access static variable, you can not access non-static. General static function is used to initialize static variables.

class A {
 public :
     static  void Fun (A A);
 Private :
     int x; 
}; 
 
void A :: Fun (A A) { 
    COUT << x; // to x references the wrong 
    COUT << AX; / / right 
}

Here we can have a very clear understanding of the problem of static scope.

to sum up:

1. function definition can not appear in the class vitro static keyword; 


2. Between each static members can access, including access static member functions static data members and member functions static access; 


3. any non-static member functions can access static member functions and static data members; 


4. static member function can not access non-static member functions and non-static data members;

 

Guess you like

Origin www.cnblogs.com/lcxyjst/p/11605599.html