《C++ primer 5》 chapter 2.2

来源:互联网 发布:sql create 唯一 编辑:程序博客网 时间:2024/06/01 14:57

note


Initialization is not assignment.

Initialization happens when a variable is given a value when it is created.

Assignment obliterates an object’s current value and replaces that value with a new one.


List Initialization

For example, we can use any of the following four different ways to define an int variable named units_sold and initialize it to 0:


int units_sold = 0; int units_sold = {0}; int units_sold{0}; int units_sold(0); 



The generalized use of curly braces for initialization was introduced as part of the new standard. This form of initialization previously had been allowed only in more restricted ways.

When used with variables of built-in type, this form of initialization has one important property: The compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss of information:


long double ld = 3.1415926536; int a{ld}, b = {ld}; // error: narrowing conversion required int c(ld), d = ld; // ok: but value will be truncated Default Initialization When we define a variable without an initializer, the variable is default initialized. 


Such variables are given the “default” value. What that default value is depends on the type of the variable and may also depend on where the variable is defined.

The value of an object of built-in type that is not explicitly initialized depends on where it is defined. Variables defined outside any function body are initialized to zero.

With one exception, which we cover in § 6.1.1 (p. 205), variables of built-in type defined inside a function are uninitialized. The value of an uninitialized variable of built-in type is undefined (§ 2.1.2, p. 36).


It is an error to copy or otherwise try to access the value of a variable whose value is undefined.Most classes let us define objects without explicit initializers. Such classes supply an appropriate default value for us.Some classes require that every object be explicitly initialized.The compiler will complain if we try to create an object of such a class with no initializer.


To support separate compilation, C++ distinguishes between declarations and definitions. A declaration makes a name known to the program. A file that wants to use a name defined elsewhere includes a declaration for that name. A definition creates the associated entity.


To obtain a declaration that is not also a definition, we add the extern keyword and may not provide an explicit initializer:

extern int i; // declares but does not define i int j; // declares and defines j 


Any declaration that includes an explicit initializer is a definition.We can provide an initializer on a variable defined as extern, but doing so overrides the extern. An extern that has an initializer is a definition:

extern doubli pi = 3.1416; //definition


It's an error to  provide an initializer on an extern inside a function.

Variable must be defined exactly once but can be declared many times.


The identifiers may not contain two consecutive underscores, nor an identifier begin with an underscore followed immediately by an uppercase letter.

Identifier defined outside a function may not begin with an underscore.


Variable names are lowercase.

Classes begin with an uppercase letter.


Scope:

global scope

block scope


Nested scope:

inner scope

outer scope


exercise

exercise 2.9

(a) We should define the variable first before we use it.

(b) This is a list initialization. The compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss of information.

(c) we cannot initialization two variables this way.

(d) It's ok to compile. But the value of i is 3.


exercise 2.10

#include<iostream>#include<string>std::string global_str;int global_int;int main(){std::cout<<global_str<<std::endl;std::cout<<global_int<<std::endl;int local_int;std::string local_str;std::cout<<local_str<<std::endl;std::cout<<local_int<<std::endl;system("pause");return 0;}

Run-Time Check Failure #3 - The variable 'local_int' is being used without being initialized.



exercise 2.11

(a) Its a definition because of extern

(b) It's a declration and a definition

(c) It's a declaration not a definition


exercise 2.12

(a)(b)(c) invalid

(d)(e) valid


exercise 2.13

j = 100


exercise 2.14

i=100

sum=45






0 0
原创粉丝点击