Static data members and functions

Source: Internet
Author: User
Tags first string

Static Data Member

A class is a type rather than a specific data object. class objects are instances of this class. Each class object has its own data member and is independent of each other, occupying the memory space. However, in a program, all objects of the class need to share a certain data within the scope of the class. Class Members declared as static can be shared in the scope of the class, which is called static members.

I. global variables and static data members

To share data among multiple objects of the same type, you can use static data members.

The use of static data members does not undermine the principle of hiding, that is, to ensure security.

 

Ii. static data member features

Each class has only one copy. All objects in the class are jointly maintained and the values of static data members are the same for each object, but their values can be updated. You only need to update the value of the static data member once to ensure that all objects have the same value after access updates.

Iii. Definition and initialization format of static data members

Static type identifier static data member name; // Definition

Type identifier Class Name: static data member name = initial value; // Initialization

Note:

(1) static data members belong to all objects of the class and occupy a portion of the memory space.

(2) static data members are stored in static mode, which indicates the lifetime of static data. It allocates space when the program starts compilation, rather than when an object is created. It is not released with the object revocation, but the space is released only after the program ends.

(3) initialization is performed in vitro, and static is not added before, to avoid confusion with general static variables.

(4) The access permission modifier private and public of the member is not added during initialization.

(5) during initialization, the scope operator is used to indicate the class to which it belongs. Therefore, static data members are members of the class, not members of the object.

Iv. Access to static data members

Class Name: static data member name

Code:

Class. h definition: static int noofstds; // static data member

. Cpp initialization:

Int cstudent: noofstds = 0; // static data member Initialization

Internal functions can be directly referenced. Common functions outside the class are restricted by the access permission. The access method is cstudent: noofstds = X;

Static member functions

Static member functions, like static data members, all belong to static members of the class and are not object members.

I. Definition Format of static member functions

Static function type static member function name (parameter table );

A static member function is added with the keyword static before the member function declaration.

Ii. Call static member functions in the following format

Class Name: static member function name (parameter table );

Iii. Features of static member functions

(1) Public static member functions can be called by class name or object name, while general non-static member functions can only be called by Object Name. Static member functions can be directly called by the class name through the symbol.

(2) static member functions can directly access static data members and static member functions of the class, and cannot directly ask non-static data members and non-static member functions. If a non-static member is to be referenced in a static member function, it can be referenced through an object.

Code2:

Cmystring is a class. set_total_len is the static member function, total_len is the static data member, and Len is the common member.

Method 1: pass an object reference to determine which class object a non-static member belongs.

Static int set_total_len (cmystring Str)

{

Total_len + = Str. Len;

Return total_len;

}

Call time:

Cmystring str1 ("This is the first string ");//

Cout <str1.set _ total_len (str1) <"\ n"; // or

Cout <cmystring: set_total_len (str1) <"\ n ";

Static data members and functions

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.