<<Effecitve C++>>笔记2

Chapter 2

Item 5: Compilers may implicitly generate a class’s default constructor, copy constructor, copyassignment operator, and destructor.

 

Item 6:To disallow functionality automatically provided by compilers, declare the corresponding member functions private and give no implementations. Using a base class like Uncopyable is one way to do this.

       (Uncopyable function tell us that to add a feature to an class by inherent  from an class just with that feature.)

 

Item 7:1、Polymorphic base classes should declare virtual destructors. If a class has any virtual functions, it should have a virtual destructor.

         2、Class not designed to be base classes or not designed to be used polymorphically should not declare virtual destructors.(Because virtual table pointer----效率问题)

 

Item 8: 1、Destructors should never emit exceptions. If functions called in a destructor may throw (exceptions), the destructor should catch any exceptions, the swallow them or terminate the program.

只要析构函数吐出异常,程序就肯呢过过早结束或出现未定义行为。所以一个被析构函数调用的函数可能抛出异常,析构函数应该捕捉任何异常,然后吐下(swallow)它们或者结束程序。

处理该问题可以增加一个接口,让客户去处理析构函数中需要的操作,析构函数中判断客户是否进行了该操作,未进行的话析构函数代替客户来操作。由此可以得出析构函数中的操作,最好还有对应该操作的一个接口,这是设计中的一个小细节。这样就如第2点所说,客户可以对异常做出反应。

          2、If class clients need to be able to react to exceptions thrown during an operation, the class should provide a regular(i.e., non-destructor) function that performs the operation.

 

Item 9: Never call virtual functions during construction or destruction, because such calls will never go to a more derived class than that of the currently executing constructoror destructor.

在构造函数和析构函数中是不存在动态链接的!!

 

Item 10: Have assignment operators(operator=) return areference to *this.

对于内置类型来说是可以连续赋值的,比如int a=b=c=d=1;让对象像内置类型一样使用是减少用户出错的一种手段。重载operator=的时候返回*this就可以实现像内置类型一样的连续赋值。

 

Item 11: 1、Make sure operator = is well-behaved when an object isassigned to itself. Techniques include comparing addresses of source and target objects, careful statement ordering, and copy-and-swap.

operator=存在两个问题,1)自我赋值问题2)异常安全问题。(其实归根到底是资源管理的问题)

在处理赋值操作符的时候就要考虑自我赋值的问题。尤其是选择自我管理资源的时候,很可能在还没有使用完资源之前就将该资源销毁(delete)。

1)传统的避免自我赋值的方式就是检查参数与(*)this。

       但是这种方式虽然解决了自我赋值的安全问题,但是依然不是异常安全的,因为在对象构造的时候(new)可能会产生异常(内存不足,构造函数异常)。

2)引入一个中间变量,在我们没有完成赋值之前不要删除原来的指针。

       这种方式能够解决以上两种问题。

3)运用“copy and swap”:先复制一个副本,然后调用swap函数交换。

这个方式还用更为简洁的方式,将对象直接按值传递给swap函数进行交换,因为传值的时候直接就是复制,如此可以让编译器编译出更高效的代码。

              2、Make sure that any functions operating on more than one object behaves correctly if two or more of the objects are the same.

 

Item 12: 1、Copying functions should be sure to copy all of an object’s data members and all of its base class parts.

当为类增加一个数据成员或者删除一个数据成员的时候,就必须更新很多的函数(构造函数、复制类型函数、与operator=相关的一些函数)一句话类的属性对类的实现和某些接口是有影响的。

 复制所有的数据成员包括两点:1)复制所有的局部成员2)调用基类适当的复制函数。

              2、Don’t try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function that both call.

       不要再复制函数中调用另外一个复制函数,可以将相同的部分定义为另外一个函数(这样的函数往往是private而且是命名为init),然后两个复制函数共同调用该函数。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值