Class member variable initialization location

Source: Internet
Author: User

1. Brief Introduction

Class member variables include int a, const int a, int & a, static int a, static const int a, statice int &. The first three are common variables, const variables, and reference variables. The last three are static variables added to the first three. This article mainly analyzes the locations where these six variables are initialized.

2. Description

For int, const int, and int &, each object has a copy relative to static, so each object must be constructed once. The initialization of int is unrestricted. Const int and int & are required to be initialized during definition. Therefore, it is required to be initialized only in the initialization member list by default. Static int, static const int, and static int & are unique for a class, and all objects share one, it should be initialized in the class file, and it is not suitable for initialization anywhere else. Some people may wonder whether static const int or const can be initialized in the initialization member list? No. If it is initialized in the initialization member list, the type will be reinitialized every time the constructor is called. This type is static and should not be reinitialized, but cannot be compiled, if this type is compiled, it will be initialized multiple times, which is inconsistent with const.

3. Code

# Include <iostream>
Using namespace std;

Class Test {
Int a; // initialization position: constructor initialization member list, constructor, and other functions

Const int B; // initialization position: constructor initialization member list
Int & c; // initialization position: List of initialized members of the constructor
Static int d; // initialization location: class file
Static const int e; // initialization location: class file
Static int & f; // initialization location: class file
Public:
Test (int a, int B, int c): a (a), B (B), c (c) {}// list of constructor initialization members
};
Int Test: d = 0; // class file
Const int Test: e = 0; // class file
Int & Test: f = Test: d; // class file
Int main (){
Test test (1, 2, 3 );
System ("PAUSE ");
Return 0;
}

4. References
C ++ static, const, and static const and Their initialized http://blog.csdn.net/yjkwf/article/details/6067267

5. Remarks

Static const int can also be initialized in the constructor as mentioned in the reference article. I have verified this. It is not possible in the constructor member initialization list or constructor, when compiling separately, the following error occurs: "static variables can only be initialized at their definition" and "assign values to read-only variables ". In short, as long as there is static, It is static, there can be only one copy, can only be initialized in class files.

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.