The annotation of C++ primer {藤原豆腐坊自家用}

The annotation of <<C++ primer>>

{藤原豆腐坊自家用}

给变量名一个初始值几乎总是正确的. 但不要求必须这么做

C++的主要设计目的之一就是允许程序员自定义类型,而这些类型和内置类型一样易于使用.

什么是对象?

一般而言, 对象是内存中具有类型的区域,说的具体一些, 计算左值表达式就会产生对象.

关于初始化

C++支持两种初始化方式,

复制初始化 copy initialization

直接初始化 direct-initialization

int ival(123); //direct-initialization

int ival = 123; //copy-initialization

初始化不是赋值!

C++里面非常强调赋值和初始化不是同一操作!!

初始化指创建变量并给它赋初始值, 而赋值则是擦除对象的当前值并用新值代替.

关于左值右值的定义.

1. lvalue (pronounced "ell-value"): An expression that is an lvalue may appear as either theleft-hand or right-hand side of an assignment.

2. rvalue (pronounced "are-value"): An expression that is an rvalue may appear on the right-but not left-hand side of an assignment.

关于变量初始化的规则:

Initialization of Variables of Built-in Type

Whether a variable of built-in type is automatically initialized depends on where it is defined.Variables defined outside any function body are initialized to zero. Variables of built-in typedefined inside the body of a function are uninitialized . Using an uninitialized variable foranything other than as the left-hand operand of an assignment is undefined.

Initialization of Variables of Class Type Each class may also define what happens if a variable of the type is defined but an initializer isnot provided. A class does so by defining a special constructor, known as the defaultconstructor . This constructor is called the default constructor because it is run "by default;" ifthere is no initializer, then this constructor is used. The default constructor is used regardless ofwhere a variable is defined.

注意! extern声明不是定义, 也不分配储存空间.事实上,他只是说明变量定义在程序的其他地方, 程序中变量可以声明多次, 但只能定义一次.

啥是作用域(scope)?

用来区分名字的不同意义的上下文称作 作用域.

(大牛们总能把一个很感性的概念很具体浅显的表述出来,啊 膜拜lippman)

一般局部作用域和全局作用域对于C程序员来说都是很熟悉的. 这里需要强调一下的是语句作用域(statement scope).

for(int val = 1; val< 10; val++)

这里val变量就是个语句作用域里面的变量. 它定义在for语句的作用域中,只能在for语句中是使用, 而不能在main函数中使用.

关于const的一些问题会开一贴集中整理好我遇到过所有关于const的问题 🙂

引用:

引用就是对象的另外一个名字. 不能定义引用类型的引用, 但可以定义任何其他类型的引用. (不能二次引用)

int ival = 1024;int &refVal = ival; // ok: refVal refers to ivalint &refVal2; // error: a reference must be initializedint &refVal3 = 10; // error: initializer must be an object

初始化是指明引用指向哪个对象的唯一方法.

const 引用是指向const 对象的引用.

This behavior is easiest to understand when we look at what happens when we bind a referenceto an object of a different type. If we writedouble dval = 3.14;const int &ri = dval;

the compiler transforms this code into something like this:int temp = dval; // create temporary int from the doubleconst int &ri = temp; // bind ri to that temporary

同学, 请你讲讲typedef的作用 ?

每句成员是常量. 不能改变枚举成员的值.枚举成员本身就是一个常量表达式, 所以也可以用于需要常量表达式的任何地方. 每个enum都定义一种唯一的类型. 枚举类型的对象的初始化或赋值只能通过其枚举成员或同一枚举类型的其他对象来进行.

类的数据成员:

定义变量和定义数据成员存在非常重要的区别:

当你困难失望的时候,最重要的是事瞧得起你自己;

The annotation of C++ primer {藤原豆腐坊自家用}

相关文章:

你感兴趣的文章:

标签云: