C + + Initialization

Source: Internet
Author: User

1. Relationship of declaration and definition

A declaration makes a name known to the program, and a file must contain a declaration of that name if it wants to use a name defined elsewhere. The variable declaration specifies the type and name of the variable, which, in addition to the same point, requests storage space for the variable, and may also assign an initial value to the variable. If you want to declare a variable rather than define it, add the keyword extern in front of the variable name and do not display the initialization variable:

extern int i;//declaration I rather than definition I

int i;//declares and defines I

Any declaration that contains an explicit initialization becomes a definition, even if there is an extern keyword:

extern double pi=3.14;//definition

A variable can be defined only once, but may be declared more than once.

2. Initialization and its way

When the object is created with a specific value, we say that the object is initialized.

(1) Default initialization

If you do not specify an initial value when defining a variable , the variable is initialized by default, at which time the variable is given the default, and the default value is determined by the defined variable type and the defined position. If it is a built-in type , its value is determined by its position, and variables defined outside of any function are initialized to 0, and the value of the variable defined inside the function body is undefined. Each class determines how it initializes the object, and whether the object is allowed to be defined without initialization is also determined by the class itself.

(2) List initialization

Initialize the variable with curly braces ({}), and if we initialize it with a list and the initial value has the risk of losing information, the compiler will say:

a long double ld=3.1416926536;

int a{ld},b={ld};//error, conversion not executed because there is a risk of missing information

int C (LD), d=ld;//Correct, conversion execution, and indeed missing part of the value

(3) Copy initialization

If you initialize a variable with an equal sign (=), the actual copy initialization is performed, and the compiler copies the value to the right of the equals sign to the newly created object:

String s5= "Hello,world";//Copy initialization

(4) Direct initialization

If you do not use the equals sign, it is the direct initialization:

String S5 ("Hello,world");//Direct initialization

when the initial value is only one, the use of direct initialization or copy initialization is OK, if there are multiple values to use, it is generally only possible to initialize directly :
String S7 (, ' C ');

(5) Value initialization

For example, when a vector object is provided with only the number of elements that the object holds, and the initial value is omitted, the library creates an initial value of the initialized element and assigns it to all the elements in the container:

Vector<int> Ivec (10);//10 elements, with each element initialized to 0

There are two special limitations to this type of initialization:

If the type of the element in the vector object does not support silent initialization, we must provide the initial element value.

If you provide only the number of elements without setting an initial value, you can only use direct initialization.

(6) initial value in class

C++11 the new standard stipulates that a data member can be given a class initial value, when an object is created, the inner class initial value is used to initialize the data members in the class, and the member without the initial value is initialized by default.

struct s{

Std;:string Bookno;

Units_sold=0;

Double revenue=0.0;

};

Initialization is bookno initialized to an empty string, and both Units_sold and revenue are initialized to 0.

Limitations on the initial values within a class: either put in curly braces or on the right side of the equals sign, remembering that parentheses are not allowed.

Integral

C + + Initialization

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.