Summary of C + + static member functions

Source: Internet
Author: User
Tags volatile

The static members of classes in C + + (C + + training) are really a love-hate feature. I decided to summarize the knowledge points of static class members so that I would not be passive on such issues in future interviews.

Static class members include both static data members and static function members.

A static data member:

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

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 (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;//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;< p= "" >

b._num++;

cout<< "derived class static data number _num is" <<b._num<<endl;< p= "" >

}//The result is 1, 2; The derived class has a static data member that is shared with 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 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, but ordinary data members are not. 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, still data member

Base _object2;//Error

Base *pobject;//Correct, pointer

Base &mobject;//Correct, reference

}; 5.★ This feature, I don't know if it's a feature in standard C + + , or VC6 own feature.

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

  static member functions there is nothing too good to say.

1. The address of a static member function can be stored using a normal function pointer, whereas a normal 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 can not 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:

Class base{

Virtual static void Func1 ();//Error

static void Func2 () const;//error

static void Func3 () volatile;//error

};

The last thing to say is that static members inC + + can be accessed independently, that is, without creating any object instances.

Summary of C + + static member functions

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.