169人阅读 评论(0)

# Chapter 5 . Expressions Summary

## 1.  Bool

• Because bool converts to one , is almost never right to write an equility test that test against the boot literal TRUE.

if ( val == TRUE) {/********/}

More importantly, if val is not bool , then comparing val with TRUE is equivalent to writing -----if(val == 1){}

which is very different from if ( val ) { /************/}

## 2. Assignment Operators

• Array names are nonmodifiable left-values
• Assignment is right associative

int ival,ival2;

ival = ival2 = 0;//ok: each assigned 0

• Assignment has low precedence

while(  (i = get_value() )!= 42) {     /*   */  }

Note : The additional parentheses around the assignment are necessary because assignment has low precedence than inequality.

## 3. Increment and Decrement Operators

• Best Practice

Use postfix operators( value++/value--) only when necessary

The reason is simply: The prefix do less work

The prefix( ++/--value ) : increment or decrement the value and returns the result version

The postfix operator : 1. Store the original values

2. Return the unincremented  or undecremented value

3. More complex iterator types, this extra work potentially could be most costly

## 4. Operator Precedence

• unary operator one:

++(postfix),--(postfix),typeid(type ID),typeid(run-time type ID), explicit cast(static_cast and on) (Right associate)

• unary operator two:

++,--,~,+,-,!,*,&,new,delete,new[],delete[] (Right Associate)

•   *, / , % , +(add) , -(subtract), << , >>           (Left   Associate)
•   > , >=, < , <= , !=, ==, &, ^ , |                       (Left   Associate)
•   &&, ||, ?:                                                       (Left   Associate)
•  =, /= , &=,......                                                (Right Associate)
• ,                                                                      (Left   Associate)

## 5. Initialization

• Just as we always initialize the objects we define as variable, it is always a good idea to initialize dynamically allocated objects.

string *ps = new string(); //initialized to empty string

int *pi = new int(); pi points to an int value-initialized to 0

int *pi = new int  ; pi points to an int value-uninitialized int

## 6. New  operator throw exception(bad_alloc)

• If the new expression can't acquire the requested memory, it throws an exception named bad_alloc.

## 7. Delete operator

• Deleting a pointer that addresses memory that was not allocated by new is undifined.

~~~~It is illegal to application.

• The C++ language guarantees that deleting a pointer that is equal to zero is safe.

~~~~int *ip = 0;

~~~~delete ip; //ok:always ok to delete a pointer that is  equal to 0

• Dangling Pointer

After deleting a pointer , the pointer becomes what is referred to as a dangling pointer.

Best Practices:

Setting the pointer to 0 after the object it refers to has been deleted.

Delete a pointer to dynamically allocated memory , the memory returned to the free store.

## 8.  Managing Dynamic Memory

•  Failure to delete dynamically allocated memory is spoken of as a "memory leak"
• Reading or writing to the object after it has been deleted.
• Applying a delete expression to the same memory location twice.

## 9. Arithmetic Conversions

• Understanding the arithmetic conversions

bool                 flag;            char                     cval;

short               sval;           unsigned short     usval;

int                    ival;           unsigned int         uival;

long                lval;            unsigned long     ulval;

float                fval;            double                dval;

......... 3.141592L+'a'; // promote 'a' to int, then conver to long double

......... dval + ival; // ival converted to double

......... dval + fval;// fval converted to double

......... ival = dval; // dval converted (by truncation) to int

......... flag = dval;// if dval is 0, then flag is false , otherwise true

......... cval + fval;// cval promoted to int , that int converted to float

......... sval + cval;// sval and cval promoted to int

......... cval + lval; // cval coverted to long

......... ival + ulval;// ival converted to unsigned long

......... usval + ival; // promotion depends on size of unsigned short and int

......... uival + lval; // conversion depends on size of unsigned int and long

## 10. Explicit Conversions

An explicit conversion is spoken of as a cast and is supported by the following set of named cast operators:

• static_cast,dynamic_cast,const_cast,reinterpret_cast
• const_cast:

A const_cast , as its name implies , casts away the constness of its expression.

Only a const_cast can be used to cast away constness.

By using a cast, the programmer turns off or dampens normal type-checking

The advice is particularly important regarding use of reinterpret_casts.  Such casts are always hazardous. A interpret_cast is inherently machine-dependent.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：4436次
• 积分：159
• 等级：
• 排名：千里之外
• 原创：10篇
• 转载：1篇
• 译文：1篇
• 评论：0条
文章分类
文章存档