static method call

1.static class members

C ++ primer inside said, static class members unlike ordinary class data members, static data members of the class independent of all objects in the class. static class data members are associated with the class,

But the object does not have any relevance to the definition of the class. That is not like an ordinary class static data members as each object has a class, all class object is a static class members shared.

A modified example of the class object to a static member, the static member of the class value of the object corresponding to the object B is also 1.

static class members do not take up space object

#include <iostream>

using namespace std;

class A{
    public:
        A(){} 
        ~A(){} 
    private:
        static int x;  //static int x=1;错误 
        
        static void m_data(int a)
        {  x=a;  }     
};

int A::x=1;

int main()
{
    A my_class;
    cout<<sizeof(my_class)<<endl; 
    return 0;
}
View Code

 

Note: static class object must be initialized outside of the class

class the Text   
{   
    public :  
     static  int COUNT;   
};   
  
int the Text :: COUNT = 0 ; // with the static member variables must be initialized    
  
int main ()   
{   
    the Text T1;   
    COUT << t1.count << endl;   
     return  0 ;   
} // program output 0

static variables modified before the object exists, so static variables to be modified outside the class initialization. Because static variables are shared by all objects must be better than the pre-existing objects.

class the Text   
{   
    public :  
     static  int COUNT;   
};   
  
int the Text :: COUNT = 0 ; // with the static member variables must be initialized    
  
int main ()   
{   
    the Text T1;   
    the Text T2;   
      
    t1.count = 100 ;      // T1 to objects static members to count 100    
    COUT t2.count << << endl; // when printing the object a static member t2, the display 100 is not 0    
    return  0 ;   
}

2.static class member functions

 

Since the modified static class members belong to the class, are not covered, so there is no static class member functions of this pointer, this pointer is a pointer pointing to this object. Because there is no this pointer, static member function

Can not access non-static class member can be accessed only modified static class members.

class the Text   
{   
    public :  
     static  int Fun ()   
    {   
        return NUM;   
    }   
    static  int COUNT;  
     int NUM;   
};   
int the Text :: = COUNT . 5 ; // with the static member variables must be initialized    
  
int main ()   
{   
    the Text T1;   
    the Text T2;   
    t1.num = 100 ;   
      
    t1.fun (); // an error occurred, fun non-static class member function if the return count would return the correct    
    return  0 ;   
}

Call the method:

  (1) class object by calling
  (2) by calling the class name

#include <the iostream> the using namespace STD;
 class the Account {
     public :
         static Double m_rate;     // declaration: independent object class static void set_rate ( const Double & X) { 
              m_rate = X; 
        } 
}; Double the Account :: = m_rate 1.0 ;     // set up the initialization, setting the initial value (defined: obtaining memory) int main () 
{ 
    the Account :: set_rate ( 5.0 );       // name of the calling class 
    cout << Account :: m_rate <<

  
          
    



 endl;
    A the Account; 
    a.set_rate ( 7.0 );     // object to call a static member function, the compiler will no longer be added to the parameters in this indicator, the implicit operation 
    cout << << m_rate the Account :: endl;
     return  0 ; 
}

 

 

Guess you like

Origin www.cnblogs.com/xiaokang01/p/12453623.html
Recommended