Definition and initialization of static data members and const data members

Source: Internet
Author: User

initialization of three types of data
    • Initialization of 1.static int a
    • Initialization of Const int a
    • Initialization of the static const int a
three ways of initializing
    • Initializing outside the class
    • Class in the constructor by initializing the list.
    • initializing at the same time as the declaration
A. static data member    Static member: A member in a static class that joins the static modifier, which is a static member. You can access this static member directly using the class name. static member name. because static members exist in memory, non-static members need to be instantiated to allocate memory, so static member functions cannot access non-static members: Because static members exist in memory, non-static member functions can directly access static members of the class.a static data member has only one copy in the program and is shared by all objects of that type. static data members are stored in the global data area . static data members are defined when they are allocated space, so they cannot be defined in a class declaration . It does not belong to a particular class object, and its scope is visible when no class object is produced, that is, we can manipulate it without producing an instance of the class;the static data member is initialized in the following format:
Data type >< class name >::< static data member name >=< value
The static data members of a class are accessed in two ways:
Class object name >.< static data member name or class type name >::< static data member name

two. const data member initialization modeInitialization by constructor list, cannot be initialized in constructor

Summary:static int A to initialize the const int A in the constructor initializer list to initialize the static const int A can be initialized at the time of declaration, or it can be initialized outside the class. However, you cannot initialize the list by constructor initialization.

Static data members and const data members are defined and initialized

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.