C + + static member variable and static member function usage Summary __jquery

Source: Internet
Author: User
Tags volatile

Summary of the use of C + + static member variables and static member functions:

one. Static member Variable:
A data member in a class body is preceded by a static keyword, and the data member becomes a statically data member of the class. As with other data members, static data members also adhere to public/protected/private access rules. At the same time, static data members also have the following characteristics:

1. Definition of static data members.
A static data member is actually a global variable in a class domain. Therefore, the definition (initialization) of a static data member should not be placed in a header file.
It is defined in the same way as a global variable. Examples are as follows:
Xxx.h file
Class base{
Private
The static const int _i;//declares that standard C + + supports ordered types to be initialized in the class body, but VC6 is not supported.
};

Xxx.cpp file
The const int base::_i=10;//definition (initialization) is not subject to private and protected access restrictions.

Note: Do not define (initialize) static data members in the header file. In most cases, doing so will cause duplicate definitions of such errors. Even if you add #ifndef #define #endif或者 #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 members of the base class with the base class object. Examples are as follows:
Class base{
Public:
static int _num;//Declaration
};
int base::_num=0;//true definition of static data members

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 result is 1, 2, and it is visible that the derived class has a static data member in common with the base class.

3. Static data members can be optional parameters for member functions, while ordinary data members may not. Examples are as follows:
Class base{
Public:
static int _staticvar;
int _var;
void foo1 (int i=_staticvar);/correct, _staticvar as static data member
void Foo2 (int i=_var);//error, _var as normal data member
};

4. The type of the static data member can be the type of the owning class, while the normal data member is not available. A pointer or reference to a normal data member that can only be declared as the owning class type. Examples are as follows:

Class base{
Public:
Static base _object1;//correct, statically data member
Base _object2;//Error
Base *pobject;//correct, pointers
Base &mobject;//correct, referencing
};

5. This feature, I do not know is the standard C + + features, or vc6 their own characteristics.
The value of a static data member can be legitimately changed in a 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 member function

_i++;//correct, mutable data member
_staticval++;//correct, static data member
_val++;//Error
}
};
int base::_staticval=0;

two. Static member functions:

1. The address of a static member function can be stored as a normal function pointer, whereas the normal member function address needs to be stored with a class member function pointer. Examples are as follows: &NBSP
class base{ 
static int func1 ();  
int Func2 ();  
}; 

Int (*PF1) () =&base::func1;//normal function pointer  
Int (base::* pf2) () =&base::func2;//member function pointer  

2. Static member functions can not invoke non-static members of a class. Because the static member function does not contain the this pointer. &NBSP
3. Static member functions cannot be declared as virtual, const, or volatile functions at the same time. Examples are as follows: &NBSP
class base{ 
virtual static void Func1 ();//error  
static void Func2 () const;//error  
static void func3 () volatile;//error  
}; 
          The statics function does not contain the hidden this pointer provided by the compiler. It exists when the class is not instantiated, so it can be invoked directly with the   (class Name:: function)   , and because there is no this pointer, there is no specific member variable for it, because the class member variables do not exist until they are instantiated. The system also does not allocate space to these variables, and there is no this pointer and cannot call these member variables, so he can only use static variables that already exist before the class is instantiated. The last thing to say is that static members can be accessed independently, that is, you can access them without creating any object instances.

A normal member function, that is, a non-static function, is passed a default this pointer when new. So it means that each object has a set of its own member variables, which means that he can use these member variables, as well as static member variables, because the variables already exist before the object is new.

Normal member functions are called by objects, so they require an object to be created first, and static member functions can be used without creating an object. Therefore, member functions independent of the non-static data members of a class, although they can be defined as non-static functions, are more convenient to use if defined as static functions. In addition, if a member function of a class wants to be used as a callback function, it can generally be defined as a static member only.

Note: When instantiated, a class is made with the New keyword, and new will provide a hidden this pointer by default, as long as it is used to access the member variables of the instance object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.