Static variables in the C ++ class

Source: Internet
Author: User

The following is a description of static variables in the class.
========================================================== ========
The usage and precautions of static data members are as follows:

1. When defining or describing static data members, add the keyword static.

2. static member Initialization is different from general data member initialization. The format of static data member Initialization is as follows:

<Data type> <class name >:< static data member name >=< value>

This indicates:

(1) initialization is performed in the external class without static before, so as to avoid confusion with general static variables or objects.

(2) during initialization, the access control letter private and public of the member are not added. Even if the static variable is private, it can still be initialized in the class in vitro (in fact, it can only be defined in the class in vitro), but it cannot be accessed in the main function. If it is not initialized, a link error may occur at the runtime.


(3) 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.

3. static data members are stored in static mode, which indicates the lifetime of static data and must be initialized.

4. When referencing static data members, use the following format:

<Class name >:: <static member name>

If the access permission of the static data member is allowed (that is, the Public Member), you can reference the static data member in the program in the preceding format.

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.