Summary of Use of C + + static member variables and static member functions

Source: Internet
Author: User

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

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

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

Xxx.cpp file
const int base::_i=10; The definition (initialization) is not restricted by private and protected access.

Note: do not define (initialize) static data members in the header file. In most cases, doing so would cause a duplicate definition of such an error. Even adding #ifndef #define #endif或者 #pragma once.

2. Static data members are shared by all objects of the class, including objects of the class's derived classes. That is, a derived class object shares a static data member of a base class with a base class object. Examples are as follows:
class base{
Public:
static int _num;Statement
};
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; a static data member is shared by the derived class and the base class.

3. A static data member can be an optional parameter to a member function, while a normal data member may not. 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 as normal data member
};

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

class base{
Public:
Static base _object1;Correct, static data member
Base _object2;Error
Base *pobject;Right, pointer
Base &mObject;Correct, reference
};

5. This feature, I do not know whether it belongs to the standard C + + features, or VC6 own features.
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 functions

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

two. Static member function:

1. The address of a static member function can be stored using a normal function pointer, whereas an ordinary member function address needs to be stored with a class member function pointer. Examples are as follows:  
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 cannot invoke non-static members of a class. Because the static member function does not contain the this pointer.  
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  
}; 
          static function does not contain a hidden this pointer provided by the compiler. It exists when the class is not instantiated, so it can be used directly with &NBSP; (class Name:: function)   to invoke, and because there is no this pointer, there is no specific member variable for it to use, because the class is not instantiated before the class member variables do not exist, the system does not allocate space to these variables, and there is no this pointer, you cannot call these member variables, So he can only use static variables that have existed before the class was instantiated. The last thing to say is that static members can be accessed independently, that is, without creating any object instances.

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

An ordinary member function is called by an object, so it is required to establish an object first, whereas a static member function can be used without an object. Therefore, member functions that are independent of the non-static data members of a class can be defined as non-static functions, but are more convenient to use if defined as static functions. In addition, if the member function of a class wants to be used as a callback function , it is generally only possible to define it as a static member.

Note: when the class is instantiated, it is done 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 .

Summary of Use of C + + static member variables and static member functions

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.