Timothy

链接

《More Effective C++》------ ( 1 )
《More Effective C++》------ ( 3 )

《More Effective C++》------ ( 2 )

Timothy posted @ 2010年6月08日 01:10 in Reading Notes , 1162 阅读

 条款6:区别increment / decrement操作符的前置和后置形式

(1):prefix和postfix语法上有区别,因为他们的函数形式表面上一样的,于是实际上 postfix 是多了一个参数的,值始终为0而已。

      并且,prefix返回引用,postfix返回const对象(代表旧值,const是避免连加)。

      int& operator++() .............                ++i;

      const int operator++( int ).............   i++(0);

(2):从效率上说,prefix比postfix高;从维护上说,一般的实现版本中,postfix代码调用prefix函数来完成累加动作,这样维护 加动作 只需要维护prefix的函数。

 

条款7:千万不要重载&&, ||和,操作符

(1): &&, || 有一定的提前判断,  ,操作符表示顺序执行,返回最后一个操作的值

(2):该条款告诉你,某些operator不要重载,会破坏C++的语言特性。

不可重载的有: 

.       .*          ::            ?:

new  delete sizeof  typeid

4个cast

 

条款8:了解不同意义的new 和 delete

(1):new operator, 即new expression,    CA *p = new CA; 展开后为:  void *buffer = operator new ( size ),  在buffer上构建CA, 调用CA的构造函数

      这里的operator new 确实存在, #include <new> ( stl ), void* operator new( size_t  t );

(2):new 和 delete 对应, 分配内存并调用构造函数,  析构再删除内存。

      如果采用operator new 取得内存,在此内存上产生对象,则不要调用delete, 只需要调用析构,再free内存。 称为 placement new。

 

void *pBuffer = operator new( sizeof( CA ) );
if ( pBuffer != NULL )
{
    CA *pA = new( pBuffer ) CA();  //pA正常

    //释放方式
    //delete pA;		//错误
    pA->~CA();
    free( pBuffer );  
} 
...

 

 

(3):数组, new[ ] 和 delete[ ] 要对应, 调用的constructor 和 destructor数量跟数组的元素个数相关。

(4):new operator和delete operator属于内建操作符,不可修改其行为;但是 operator new 和 operator delete 则可修改其行为。

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

异常(Exception)

Exception的好处和理由:

(1):返回错误码的方式,需要函数调用者检查错误,可以被跳过或者忽略掉。而抛出exception,若未被catch,则程序会马上停止;

(2):exception发生时,栈上对象可以析构掉,其他方法无法保证。

 

 

条款9:利用Destructor避免资源泄露

(1):本条款的思想就是: 利用exception发生时,析构可以自动调用,避免内存泄露。

      容易出错的代码:

 

...
CA *pA = new pA;
pA->dosomething();
delete pA;
...

 

 若dosomething()发生异常,则delete无法执行到;

 

      正确的代码:

 

...
CAObject obj( new CA );
obj.GetPtr()->dosomething();
...

 

     用一种类似于智能指针的思想,以局部变量出现,在析构中清理资源。

     

Avatar_small
Punjab Plus One Prev 说:
2022年8月24日 21:52

The Punjab Board Class 11th Important Model Question Paper 2023 would be helpful to those who desire to succeed in the Class 11 Punjab Board Exam. Due to a more precise indication of the assignments and tests they will be expected to complete during the academic year, students will be better equipped to handle the Punjab Board 11th Grade. <a href="https://www.modelpaper2020.in/punjab-1-model-paper-202/">Punjab Plus One Previous Paper 2023</a> It will be important for students to participate in its creation, and a well-designed Subjects Wise Important Model Question Paper 2023 will work to make it as interactive as possible by including students in the process of making it.

Avatar_small
Grade 8 Result chitt 说:
2022年9月03日 22:58

Chittagong is also another best education board under all education board Bangladesh, and this is also one of the divisions under eight education boards of the country, the Secondary and Higher Secondary Education Board has successfully completed those Junior School Certificate and Junior Dakil (Grade-8) annual final examination tests between 2nd to 11th November 2022 with the same schedule of all education board. Grade 8 Result chittagong The School Education Department has announced there are lakhs of students are appeared and participated in the JSC & JDC terminal examinations 2022 from all districts of Chittagong division, the Grade 8th standard examinations are successfully completed and the students are waiting to get JSC Result 2022 with total marksheet with subject wise marks.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter