Class static members and static member functions in C + +

Source: Internet
Author: User
Tags class definition

(1) Why do I need class static members?

Sometimes multiple objects of a class need to access a global object, in which case "providing a global object" is more efficient than "Maintaining a separate data member for each class". Static members of a class have two more benefits than global variables: (a) There is no possibility of conflict with other global names in the program, and (b) can be set to private to realize information hiding.

(2) Characteristics of class static members

For non-static data members, each class object has its own copy, while static data members have only one copy of each class type. Because static data members are assigned to the global data area and therefore must exist when the program starts running, the spatial allocation and initialization of static data members cannot be done in the main function or other functions (and static data members are not allowed in the local class). As a result, the spatial allocation and initialization of static data members can only be in the following three ways: (a) The header file of the class: Here are the declarations of the classes. But there is an unavoidable problem: for static data members, there can be only one definition in the program, and the header file of the class may be repeatedly referenced and duplicate defined; (b) The global data Declaration and definition at the front of the main function: This method is also problematic, Each program that uses the class must define the static members of the class here, which is unrealistic; (c) The internal implementation of the class definition: This is the ideal way to include only the header file when referencing.

#include "account.h"
double Account::_interestRate = 0.05

(3) Definition of static member of class

There are several considerations when defining static data members: (a) when defined outside a class definition, the name of a static member must be qualified by its class name, before it can be added static; (b) static data members of an ordered type such as int can be initialized in the class definition, but still need to be defined outside the class definition. However, the initial value cannot be specified at this time;

// 头文件
class Account
// ...
static const int nameSize = 16;
static const char name[nameSize];
// 文本文件
const int Account::nameSize; // 必需的成员定义,此处不可再进行初始化
const char Account::name[nameSize] = "Saving Account";

(4) Access to static members of a class:

Static data members of the class can be accessed directly in a member function of a class, and access can be restricted by using a member access operator or a class name in a non-member function.

(5) Static member function:

Static member functions cannot access non-static data members belonging to a specific class object, nor can they access non-static member functions, he can only access static data members and invoke the rest of the static member functions. This prevents the Non-static data members of the class from being tampered with. Static member functions do not have this pointer, and any explicit or implicit reference to this pointer in a static member function will result in a compile-time error.

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: 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.