Summary of the usage of static member variables and static member functions by C++

  • 2020-05-12 02:55:48
  • OfStack

1. Static member variables:

A data member in the body of a class becomes a static data member of the class when the static keyword is added before its declaration. Like other data members 1, static data members also follow the public/protected/private access rules. At the same time, static data members also have the following characteristics:

1. Definition of static data members.

Static data members are actually global variables in the class domain. Therefore, the definition (initialization) of static data members should not be placed in the header file.

It is defined the same way as a global variable. Examples are as follows:

xxx h file


class base{ 
private: 
static const int _i;// Declaration, standard c++ Supports the initialization of ordered types in the class body , but vc6 Is not supported.  
};

xxx cpp file

const int base: : _i = 10; // definition (initialization) is not restricted by access to private and protected.

Note: do not define (initialize) static data members in header files. In most cases, this will cause errors such as duplication of definitions. Even if you add #ifndef #define #endif or #pragma once.

2. Static data members are Shared by all objects of the class, including objects of the derived class of the class. That is, the derived class object shares the static data member of the base class with the base class object.

Examples are as follows:


class base{ 
public : 
static int _num;// The statement  
}; 
int base::_num=0;// The true definition of a static data member 

class derived:public base{ 
};

main() 
{ 
base a; 
derived b; 
a._num++; 
cout<<"base class static data number _numis"<<a._num<<endl; 
b._num++; 
cout<<"derived class static data number _numis"<<b._num<<endl; 
} //  The results for 1,2; You can see that derived classes are Shared with base classes 1 Static data members. 

3. Static data members can be optional parameters of member functions, while ordinary data members cannot. Examples are as follows:


class base{ 
public : 
static int _staticVar; 
int _var; 
void foo1(int i=_staticVar);// correct ,_staticVar Is a static data member  
void foo2(int i=_var);// error ,_var Is a normal data member  
};

4. The type of a static data member can be the type of the class to which it belongs, while a normal data member cannot. A normal data member can only be declared as a pointer or reference to the class type to which it belongs. Examples are as follows:


class base{ 
public : 
static base _object1;// Correct, static data member  
base _object2;// error  
base *pObject;// Correct, pointer  
base &mObject;// Correct, quote  
};

5. I don't know whether this feature belongs to the standard c++ or to vc6 itself.

The value of the static data member can be legally changed in the const member function.

Examples are as follows:


class base{ 
public: 
base(){_i=0;_val=0;}

mutable int _i; 
static int _staticVal; 
int _val; 
void test() const{//const  A member function 

_i++;// Right, mutable Data members  
_staticVal++;// Right, static Data members  
_val++;// error  
} 
}; 
int base::_staticVal=0;

2. Static member function:

1. The addresses of static member functions can be stored by ordinary function Pointers, while the addresses of ordinary member functions need to be stored by class member function Pointers.

Examples are as follows:


class base{ 
static int func1(); 
int func2(); 
};

int (*pf1)()=&base::func1;// Normal function Pointers  
int (base::*pf2)()=&base::func2;// Member function pointer 

2. A static member function cannot call a non-static member of a class. Because static member functions do not contain this Pointers.

3. Static member functions cannot be declared as virtual, const, volatile functions at the same time. Examples are as follows:


class base{ 
virtual static void func1();// error  
static void func2() const;// error  
static void func3() volatile;// error  
};

Static functions do not contain the compiler provided by hidden this pointer, it is when I was in the class does not instantiate, so can directly use (class name: : function) to invoke, and because no this pointer, so there is no specific member variables for it, not because the class instantiation, the class member variables does not exist, the system also did not allocate space for these variables, and no this pointer, also cannot call these member variables, so he can only use those that existed before class does not instantiate a static variable. Finally, 1 point is that static members can be accessed independently, that is, without creating any object instances.

Ordinary member function that is not a static function because new passed a pointer to the default this, so means that every object has a group of their own member variables, which means that he can use the member variables, at the same time can also use a static member variables, because these variables in the object don't new existed before that.

Ordinary member function to be called through the object, so it is required to first establish an object; Static member functions can be used without creating an object. Therefore, a member function that is independent of the class's non-static data member can be defined as a non-static function, but it is easier to use if defined as a static function. In addition, if a member function of a class wants to be used as a callback function, it can only be defined as a static member in the 1 general case.

Note: class is instantiated through the new keyword, and new will provide a hidden this pointer by default. This pointer is only used to access the member variables of the instance object.


Related articles: