《C++ primer 5》 chapter 2.2

本文详细解释了初始化与赋值的概念,并通过实例展示了如何使用不同的方式为变量进行初始化,包括列表初始化和默认初始化。同时,讨论了变量初始化的规则、范围和类型限制,以及在不同上下文中的行为差异。

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






乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值