Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1053510
  • 博文数量: 139
  • 博客积分: 1823
  • 博客等级: 上尉
  • 技术积分: 3403
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-05 09:54
文章存档

2014年(7)

2013年(16)

2012年(48)

2011年(68)

分类: C/C++

2013-06-26 17:03:15

Essential C++ 第二章知识点如下:
1,函数调用时,传递的参数不同:

点击(此处)折叠或打开

  1. bool fibon_elem( int pos, int &elem )
  2. {
  3. ……
  4. }
  5. void main()
  6. {
  7.     if ( fibon_elem( pos, elem ))
  8. }//C++
在纯C中,是没有引用的概念的,所以不能定义fibon_elem( int pos, int &elem )这种类型的函数参数。
在C++中,函数参数中的int &elem表示引用。下面将详细介绍引用的概念。

2,C++中的引用:
A reference serves as an indirect handle to an object. We declare a reference by sandwiching an ampersand ( & ) between the type's name and the name of the reference:

点击(此处)折叠或打开

  1. int ival = 1024; // an object of type int
  2. int *pi = &ival; // a pointer to an object of type int
  3. int &rval = ival; // a reference to an object of type int
那么为什么要使用引用呢?以下两段话解释地很好:
When an object is passed to a reference parameter, the object's value is not copied. Rather, the address of the object being passed is copied. Each access of the reference parameter within the
function is an indirect manipulation of the object passed in.
One reason to declare a parameter as a reference is to allow us to modify directly the actual object being passed to the function. This is important because, as we've seen, our program otherwise may behave incorrectly. //声明一个引用可以直接更改传给函数的对象
A second reason to declare a parameter as a reference is to eliminate the overhead of copying a large object. This is a less important reason. Our program is correct; it is simply less efficient. //声明成一个引用可以不用拷贝目标对象

在作为函数参数时,引用和指针有什么不同呢?

The effect is the same: We pass the address of the object into the function rather than make a copy of the entire object. One difference, of course, is the syntax between a reference and a pointer. //效果相同,都是传递对象的地址,而不是整个对象的拷贝。第一点区别是,二者的语法不同。
A more important difference between a pointer and a reference parameter is that a pointer may or may not actually address an object. Before we dereference a pointer, we must always make sure that it is not set to 0. A reference, however, always refers to some object, so the check for 0 is unnecessary. //更重要的不同是:指针可能并不指向一个对象,在解引用一个指针前,必须确保这个指针不是空指针,而引用则一直都指向某个对象,故不用对引用进行是否为空的检查。
第三点不同是百度得到的结果:
指针与引用的另一个重要的不同是指针可以被重新赋值以指向另一个不同的对象。但是引用则总是指向在初始化时被指定的对象,以后不能改变。

3,new 和 malloc分配空间(from web):

相同点:都可用于申请动态内存和释放内存

不同点
(1)操作对象有所不同
malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符。对于非内部数据类的对象而言,光用maloc/free 无法满足动态对象的要求。对象在创建的同时要自动执行构造函数, 对象消亡之前要自动执行析构函数。由于malloc/free 是库函数而不是运算符,不在编译器控制权限之内,不能够把执行构造函数和析构函数的任务强加malloc/free。

(2)在用法上也有所不同
函数malloc 的原型如下:
void * malloc(size_t size);
用malloc 申请一块长度为length 的整数类型的内存,程序如下:
int *p = (int *) malloc(sizeof(int) * length);
我们应当把注意力集中在两个要素上:“类型转换”和“sizeof”。
malloc 返回值的类型是void *,所以在调用malloc 时要显式地进行类型转换,将void * 转换成所需要的指针类型。
malloc 函数本身并不识别要申请的内存是什么类型,它只关心内存的总字节数。

函数free 的原型如下:
void free( void * memblock );
为什么free 函数不象malloc 函数那样复杂呢?这是因为指针p 的类型以及它所指的内存的容量事先都是知道的,语句free(p)能正确地释放内存。如果p 是NULL 指针,那么free

对p 无论操作多少次都不会出问题。如果p 不是NULL 指针,那么free 对p连续操作两次就会导致程序运行错误。

new/delete 的使用要点
运算符new 使用起来要比函数malloc 简单得多,例如:
int *p1 = (int *)malloc(sizeof(int) * length);
int *p2 = new int[length];
这是因为new 内置了sizeof、类型转换和类型安全检查功能。对于非内部数据类型的对象而言,new 在创建动态对象的同时完成了初始化工作。如果对象有多个构造函数,那么new 的语句也可以有多种形式。

如果用new 创建对象数组,那么只能使用对象的无参数构造函数。例如
Obj *objects = new Obj[100]; // 创建100 个动态对象
不能写成
Obj *objects = new Obj[100](1);// 创建100 个动态对象的同时赋初值1
在用delete 释放对象数组时,留意不要丢了符号‘[]’。例如
delete []objects; // 正确的用法
delete objects; // 错误的用法
后者相当于delete objects[0],漏掉了另外99 个对象。

阅读(2436) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~