Static member of the C ++ class)

Source: Internet
Author: User

Class static members are mainly used to solve the problem of resource sharing. However, it is still quite cool to use, and it is easy to mix up, so write it here

For later use. Class static members include static data members and static function members.


I. static data members:

When the static keyword is added before the declaration of the data member in the class, 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. Definition Method

Same as global variables. As follows:

Xxx. h file lass A {private: static int x ;}; xxx. cpp file int base: x = 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. Even if

In addition, # ifndef # define # endif or # pragma once does not work either.

2. static data members are shared by all objects of the class, including objects of the derived classes of this class.

Next we will write a simple program to verify this feature. The Code is as follows:

#include<iostream>using namespace std;class Base{public:static int x;};int Base::x = 0;class Derive:public Base{};int main(){Base a;Derive b;cout << "Base::x is " << a.x << endl;a.x++;cout << "Derive::x is " << b.x << endl;system("pause");return 0;}

Run the following command:









3. A static data member can be an optional parameter of a member function, but a common data member cannot.

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 };

This is because the storage space of instance members belongs to a specific instance, and members of the same name of different instances (objects) have different storage spaces; the storage of static members

The space is fixed and irrelevant to the specific instance (object). It is shared by all instances of this class.


4. The static data member type can be the class type, but not the common data member type. A common data member can only be declared as a class

Type pointer or reference (the reason is unknown)


Ii. StaticFunction Member

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.

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. But non-static member functions can call static

Status Member


3. static member functions cannot be declared as virtual, const, and volatile functions at the same time.


Iii. Other considerations

1. Static members can be accessed independently, that is, they can be accessed without creating any object instance.

2. You can use ClassName: MemberName or ClassName. MemberName to access static members. We recommend that you use the former for non-static members. Only the ClassName. MemberName and ClassName: MemberName can be used to access instance members.

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.