「每周review」C++ Prinmer Chapter2

本文档总结了C++的基本内置类型、变量、复合类型等内容,并深入探讨了const限定符、类型处理及自定义数据结构等高级主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/*
*This is the Chapter2 Summary.In this file,the summary will divide into two segmnet
*segment 1:the technical terms from the book;
*segmnet 2:some note from every section in this chapter.
*/

1 Segment 1:technical terms

1.1 Defined Terms

address Number by which a byte in memory can be found.alias declaration Defines a synonym for another type: using name = typedeclares name as a synonym for the type type .arithmetic types Built-in types representing boolean values, characters,integers,and floating-point numbers.array Data structure that holds a collection of unnamed objects that areaccessed by an index. Section 3.5 covers arrays in detail.auto Type specifier that deduces the type of a variable from its initializer.base type type specifier, possibly qualified by const, that precedes thedeclarators in a declaration. The base type provides the common type on whichthe declarators in a declaration can build.

bind Associating a name with a given entity so that uses of the name are usesofthe underlying entity. For example, a reference is a name that is bound to anobject.byte Smallest addressable unit of memory. On most machines a byte is 8 bits.class member Part of a class.compound type A type that is defined in terms of another type.const Type qualifier used to define objects that may not be changed. constobjects must be initialized, because there is no way to give them a valueafterthey are defined.const pointer Pointer that is const.const reference Colloquial synonym for reference to const.constant expression Expression that can be evaluated at compile time.constexpr Variable that represents a constant expression. § 6.5.2 (p. 239)covers constexpr functions.conversion Process whereby a value of one type is transformed into a value ofanother type. The language defines conversions among the built-in types.data member Data elements that constitute an object. Every object of a givenclass has its own copies of the class’ data members. Data members may beinitialized when declared inside the class.declaration Asserts the existence of a variable, function, or type definedelsewhere. Names may not be used until they are defined or declared.declarator The part of a declaration that includes the name being defined andan optional type modifier.decltype Type specifier that deduces the type of a variable or an expression.default initialization How objects are initialized when no explicitinitializer isgiven. How class type objects are initialized is controlled by theclass. Objects ofbuilt-in type defined at global scope are initialized to 0; those defined atlocalscope are uninitialized and have undefined values.definition Allocates storage for a variable of a specified type and optionallyinitializes the variable. Names may not be used until they are defined ordeclared.escape sequence Alternative mechanism for representing characters,particularlyfor those without printable representations. An escape sequence is a backslashfollowed by a character, three or fewer octal digits, or an x followed by ahexadecimal number.---global scope The scope that is outside all other scopes.header guard Preprocessor variable used to prevent a header from beingincluded more than once in a single file.identifier Sequence of characters that make up a name. Identifiers are case-sensitive.in-class initializer Initializer provided as part of the declaration of aclass datamember. In-class initializers must follow an = symbol or be enclosed insidecurlybraces.in scope Name that is visible from the current scope.initialized A variable given an initial value when it is defined. Variablesusuallyshould be initialized.inner scope Scope that is nested inside another scope.integral types See arithmetic type.list initialization Form of initialization that uses curly braces to encloseone ormore initializers.literal A value such as a number, a character, or a string of characters. Thevalue cannot be changed. Literal characters are enclosed in single quotes,literalstrings in double quotes.local scope Colloquial synonym for block scope.low-level const A const that is not top-level. Such consts are integral to thetype and are never ignored.member Part of a class.nonprintable character A character with no visible representation, such as acontrol character, a backspace, newline, and so on.null pointer Pointer whose value is 0. A null pointer is valid but does notpointto any object.nullptr Literal constant that denotes the null pointer.object A region of memory that has a type. A variable is an object that has aname.outer scope Scope that encloses another scope.pointer An object that can hold the address of an object, the address one pastthe end of an object, or zero.pointer to const Pointer that can hold the address of a const object. Apointer


to const may not be used to change the value of the object to which it points.preprocessor Program that runs as part of compilation of a C++ program.preprocessor variable Variable managed by the preprocessor. The preprocessorreplaces each preprocessor variable by its value before our program iscompiled.reference An alias for another object.reference to const A reference that may not change the value of the object towhich it refers. A reference to const may be bound to a const object, anonconst object, or the result of an expression.scope The portion of a program in which names have meaning. C++ has severallevels of scope:global—names defined outside any other scopeclass—names defined inside a classnamespace—names defined inside a namespaceblock—names defined inside a blockScopes nest. Once a name is declared, it is accessible until the end of thescopein which it was declared.separate compilation Ability to split a program into multiple separate sourcefiles.signed Integer type that holds negative or positive values, including zero.string Library type representing variable-length sequences of characters.struct Keyword used to define a class.temporary Unnamed object created by the compiler while evaluating anexpression. A temporary exists until the end of the largest expression thatencloses the expression for which it was created.top-level const The const that specifies that an object may not be changed.type alias A name that is a synonym for another type. Defined through either atypedef or an alias declaration.type checking Term used to describe the process by which the compiler verifiesthat the way objects of a given type are used is consistent with thedefinition ofthat type.type specifier The name of a type.typedef Defines an alias for another type. When typedef appears in the basetype of a declaration, the names defined in the declaration are type names.


hapterundefined Usage for which the language does not specify ameaning. Knowinglyor unknowingly relying on undefined behavior is a great source ofhard-to-trackruntime errors, security problems, and portability problems.uninitialized Variable defined without an initial value. In general, trying toaccess the value of an uninitialized variable results in undefined behavior.unsigned Integer type that holds only values greater than or equal to zero.variable A named object or reference. In C++, variables must be declaredbeforethey are used.void* Pointer type that can point to any nonconst type. Such pointers may notbe dereferenced.void type Special-purpose type that has no operations and no value. It is notpossible to define a variable of type void.word The natural unit of integer computation on a given machine. Usually awordis large enough to hold an address. On a 32-bit machine a word is typically 4bytes.& operator Address-of operator. Yields the address of the object to which itisapplied.\*operator Dereference operator. Dereferencing a pointer returns the object towhich the pointer points. Assigning to the result of a dereference assigns anewvalue to the underlying object.defined.

2 Segment 2:some note

2.1 基本内置类型    基础类型

算数类型分为两类:整型(integral type,包括字符和布尔类型在内)和浮点型一个char\空间应确保可以存放机器基本字符集中任意字符对应的数字至

扩展字符集:wchart,char16t,char32t

char16t,char32t:Unicode字符集服务

utf-8:u8-char utf-8 用8位编码一个Unicode

带符号的(signed)和无符号的(unsigned)——(除去布尔型和扩展的字符型外):特殊的——字符型分为:char,signed char,unsigned char.同时char和signed char 并不一样,类型char实际上会表现为有符号型或者是无符号,具体是由编译器决定的!!————如果使用char 进行运算,为避免在不同的机器上表现出不同的数值,所以需要指定它的类型是signed char or unsigned char

2.1.1 含有无符号类型的表达式 :i=(232-1)-42+1:

负数–>正数:等于其补码(如int类型的变量z=-1,变成unsigned int,则x=(232-1)-1+1)

//当无符号数与有符号进行运算时,将会对有符号数进行转化,在有符号数进行转换的时候,如果其为负数,则取其补码!

字面值常量对应的数据类型=常量的形式和数值20 * 十进制 * 024 * 八进制 * 0x14* 十六进制 *浮点型字面值,指数部分用E或e标识

字符和字符串字面值'a' //字符字面值"Hello World!" //字符串字面值编译器在每个字符串的结尾处添加一个空字符('\0'),因此字符串的实际长度要比它的内容多1;'a'和"a",实际上含义不一样;

2.2 变量

对象是指一块能存储数据并具有某种类型的内存空间_:对于C++程序员来说,"变量"和"对象"一般可以互换使用;注意初始化和赋值的区别:初始化是在创建变量时赋予的初始值,赋值含义是把对象的的当前擦除,以一个新值取代变量的声明和定义的关系:分离式编译:——*声明*使得名字为程序所知,*一个文件如果想使用别处定义的名字则必须包含对那个名字的声明*,*定义*负责创建与名字关联的实体(定义申请了存储空间!)

标识符和变量命名规范P42作用域全局,类,命名空间,块在内层作用域中定义一个与全局或者外层作用域重名的变量,该变量名字将会被覆盖,如果要重新使用原来的变量(比如全局),则需要显式的进行访问,使用作用域操作符"::"

2.3 复合类型

声明语句的更通用定义声明=基本数据类型+声明符列表(含变量的名字)引用:引用的类型都要和与之绑定的对象严格匹配(引用本身不是一个对象,所以不能被指针指定)指针:指针的类型都要和它所指向的对象严格匹配(指针本身不能被int变量直接赋值,因为不知道指针变量本身的数据值的数据类型是什么)空指针:0 or nullptr(是一种特殊类型的字面值)

void*指针:特殊指针,可以存放任意对象的地址,但是该存放地址对应的对象是什么数据类型对象并不了解,所以这种类型指针不能直接操作指针所指的对象!!!——能做的事情比较有限:拿它和别的指针比较,作为函数的输入或输出,或赋给另一个void*指针???

Tip:对一条比较复杂的指针或引用的声明语句,从右向左阅读有助于弄清楚它的真实含义——因为离变量最近的符号对变量的类型最有直接的影响

2.4 const限定符

const 变量在文件间共享:只在一个文件中定义const,而在其他多个文件中声明并使用它如果想在多个文件之间共享const对象,必须在变量的定义之前添加extern关键字

”常量引用“和”指向常量的指针”,引用和指针指向对象的类型必须与其所引用或者所指 向对象的顶层类型一致,即,const在常量引用初始化中允许用任意表达式作为初始值,只要该表达式的结果能转换成引用的类型即可——引申出一个临时变量

2.5 处理类型

类型的别名:typedef double wages;using SI=Salesitem类型说明符auto && decltypeFile Edit Options Buffers Tools C++Help* / gives the declared type of the expression that is passed to it. auto doesthe same thing as template type deduction. So, for example, if you have afunction that re\//turns a reference, auto will still be a value (you need auto& to get areference), but decltype will be exactly the type of the returnvalue. reference fromhttp://stackoverflow.com/questions/12084040/decltype-vs-auto

引用从来都是作为其所指对象的同义词出现,只有用在decltype处是一个例外,该函数 取了引用本身的意义

2.6 自定义数据结构

数据结构是把一组相关的数据元素组织起来然后使用它们的策略和方法数据成员:每个对象有自己的一份数据成员拷贝,波波波头文件保护符为了确保头文件多次包含在源文件中仍能安全工作的常用技术是预处理其;当与处理器看到#include 标记时就会用指定的头文件的内容代替#include,预处理变量有两种状态:已定义和为定义#ifdef:当且仅当变量已定义时为真#ifndef:当且仅当变量未定义时为真#endif:执行遇到该操作指令结束

#define指令把一个名字设定为预处理变量头文件展示:

头文件即使(目前还)没有被包含在任何其他头文件中,也应该设置保护符。

Date: 2016-03-06T23:26+0800

Author: Colly

Org version 7.9.3f withEmacs version 24

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值