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 the
left-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 type defined inside the body of a function are uninitialized . Using an uninitialized variable for anything 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 is not provided. A class does so by defining a special constructor, known as the default constructor . This constructor is called the default constructor because it is run "by default;" if there is no initializer, then this constructor is used. The default constructor is used regardless of where 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 ival
int &refVal2; // error: a reference must be initialized
int  &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 reference to an object of a different type. If we write

double dval = 3.14;
const int &ri = dval;


the compiler transforms this code into something like this:

int temp = dval;  // create temporary int from the double
const int &ri = temp;  // bind ri to that temporary 


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

  • To hide the implementation of a given type and emphasize instead the purpose for which the type is used
  • To streamline complex type definitions, making them easier to understand
  • To allow a single type to be used for more than one purpose while making the purpose clear each time the type is used



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



类的数据成员:

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

          一般不能把类成员的初始化作为其定义的一部分. 当定义数据成员时, 只能指定该数据成员的名字和类型. 类不是在类定义里定义数据成员时初始化数据成员, 而是通过构造函数的特殊成员函数控制初始化.


访问标号:

      访问标号负责控制使用该类的代码是否可以使用给定的成员. 类的成员函数可以使用类的任何成员, 而不管其访问级别. 访问标号 public, private可以多次出现在类定义中, 给定的访问标号应用到下一个访问标号出现为止.


          用class和struct关键字定义类的唯一差别在于默认访问级别: 默认情况下, struct 成员为public, 而class的成员为private.






未完~ 待更新






                           二零一五年二月  摄于湖南

                           某小学公路旁的樱花(O(∩_∩)O~应该是樱花吧)~








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值