Summary of C + + static member functions

Source: Internet
Author: User

Static members in a class include static member functions and static member data in two parts

First, static data members

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 static data members

Static data members are actually global variables in the class domain, so the definition of a static data member should not be placed in the header file. It is defined in the same way as a global variable.

Xxx.h file
Class base{
Private
The 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;//is not subject to private and protected access restrictions when defined (initialized).

Note: Do not attempt to define static data members in the header file, and in most cases this will cause a duplicate definition of the error. 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 class's derived classes. That is, a derived class object shares a static data member of a base class with a base class object.

Class base{
Public:
static int _num;//Declaration
};
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 _num is" <<a._num<<endl;
b._num++;
cout<< "derived class static data number _num is" <<b._num<<endl;
}
The result is 1, 2; a static data member is shared by the derived class and the base class.

3. Static data members can be associated with optional parameters called member functions, while ordinary data members are not.

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 a static data member can be the type of the owning class, while a normal data member cannot. A normal data member can only declare a pointer or reference to the owning class

Class base{
Public:
Static base _object1;//correct, still data member
Base _object2;//Error
Base *pobject;//Correct, pointer
Base &mobject;//Correct, reference
};

5. The value of a static data member can be legitimately changed in a const member function

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;

Second, static member functions

1, the address of the static member function can be stored with the ordinary function pointer, and the ordinary member function address needs to use the class member function pointer to store

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 static member functions do not contain the this pointer

3, static member functions can not be declared as virtual, const, volatile function.

The last point to note is that static members can be accessed independently, that is, without creating any object instances.

C + + static member function summary (GO)

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.