Summary of C ++ static member functions

Source: Internet
Author: User

The static members in the class are really a fascinating feature. I decided to summarize the knowledge points of static class members so that I would not be passive in future interviews.
Static class members include static data members and static function members.

A static data member:

When the static keyword is added before the declaration of the data member in the class body, the data member becomes the static data member of the class. Like other data members, static data members also comply with public/protected/private access rules. Static data members also have the following features:

1. Definition of static data members.
Static data members are actually global variables in the category. Therefore, the definition (initialization) of static data members should not be put in the header file.
The definition is the same as that of global variables. Example:

Xxx. h file
Class base {
Private:
Static const int _ I; // declaration, standard c ++ supports sequential type initialization in the class body, but vc6 does not.
};

Xxx. cpp File
Const int base: _ I = 10; // when defining (initialization), private and protected access is not restricted.

Note: do not try to define (initialize) static data members in the header file. In most cases, this will lead to repeated definitions of such errors. Not even if # ifndef # define # endif or # pragma once is added.

2. static data members are shared by all objects of the class, including objects of the derived classes of the class. That is, the derived class Object shares the static data member of the base class with the base class object. Example:
Class base {
Public:
Static int _ num; // Declaration
};
Int base: _ num = 0; // real definition of static data members

Class derived: public base {
};

Main ()
{
Base;
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 or 2. The derived class and the base class share a static data member.

3. The static data member can be an optional parameter of the member function, but not a common data member. Example:
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 common data member.
};

4.★The type of static data members can be the type of the class to which they belong, whereas that of common data members is not. A common data member can only be declared as a pointer or reference of the class type. Example:

Class base {
Public:
Static base _ object1; // correct, static data member
Base _ object2; // Error
Base * pObject; // correct, pointer
Base & mObject; // correct, reference
};

5.★I don't know whether it is a feature in Standard c ++ or a feature of vc6.
The value of the static data member can be legally changed in the const member function. Example:

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;

2. static member functions
There is nothing to say about static member functions.

1. The address of the static member function can be stored by the common function pointer, while the common member function address needs to be stored by the class member function pointer. Example:
Class base {
Static int func1 ();
Int func2 ();
};

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

2. static member functions cannot call non-static members of a class. Because the static member function does not include the this pointer.

3. static member functions cannot be declared as virtual, const, and volatile functions at the same time. Example:
Class base {
Virtual static void func1 (); // Error
Static void func2 () const; // Error
Static void func3 () volatile; // Error
};

The last point is that static members can be accessed independently, that is, they can be accessed without creating any object instance.

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.