C ++ static member summary, C ++, static

Source: Internet
Author: User

The static members in the class are really a fascinating feature. During the interview, I was caught by the examiner for a while, and I was beaten. So after I came back, I decided to sum up the knowledge points of the static class members so that I could not be passive on such issues during 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
};

// Neiloid note: this is because the pointer and reference space are fixed, so you can allocate. Why can static members ?? Check this information!

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.