天天看點

c++ effective心得

1.Operator overloading

一般的:  c& c::operator=(const c&){。。。。return *this;};   --------格式  運算符的重載

   w = x = y = z = "hello";//可以傳回引用,效率高

   a+b+c+d     //必須傳回類,而不是傳回引用

   c c::operator=(const c &){ 。。。。。。return *this;};

Operators that can be overloaded

+, -, *, /, %, ^, &, +=,-=,*=,/=, (), [], >>, ==,=,->,++,--, ->*, … new, delete

Operators that can not be overloaded

., ::, ?:, .*

//Notes for ++, -- operator

差別:

 T& operator ++() //prefix ++a

 T operator++(int) //postfix a++

一般情況下幾乎總要遵循operator=輸入和傳回的都是類對象的引用的原則.

必須傳回一個對象時不要試圖傳回一個引用,當需要在傳回引用和傳回對象間做決定時,你的職責是選擇可以完成正确功能的那個。至于怎麼讓這個選擇所産生的代價盡可能的小,那是編譯器的生産商去想的事。

2.

const成員函數不被允許修改它所在對象的任何一個資料成員。mutable在處理“bitwise-constness限制”問題時是一個很好的方案,但它被加入到c++标準中的時間不長,是以有的編譯器可能還不支援它。

explicit表示必須顯示的調用該函數。

3.void including more times

#ifndef

#define

#endif

等價于

#progma once

4  .struct

                             #pragma pack(pop, n)

struct Employee{

 char cName[5];

 short  nAge;

        float  dSalary;

 bool  bMarried;

};

sizeof(Employee) depends on struct member alignment. Default is 4 bytes alignment. sizeof(Employee) = 16

2 bytes data wants to start from even address, 4 bytes data wants to start from address that is 4 times, 1 byte data can start from any address

我們可以用以下改變預設的對齊方式。

#pragma pack(pop, n)

5。enum :It can define a set of const values.

6。In fact, C++ compiler creates a global function for each member function, and add another parameter whose type is our class. 一般位類名_函數名_參數清單

7。constructor   ※   deconstructor

Copy Constructor 

Copy constructor is a constructor that has a parameter whose type is class reference.

and the class reference is const.

eg:

CRectangle( const CRectangle& other)

eg:

   CComplex a;

   CComplex b=a; // Copy constructor is invoked

   b=a; //Assignment operator is invoked

Converting Constructor

一般定義位explicit函數,必須被顯示的調用

8。const in class

  4 cases in a class

 const int member;隻能在初始化清單裡進行初始化

 int funct( const int nFactor,…);參數位常熟,在函數種不可被修改

 const int func(…); 傳回的值位常數不可以再被指派和修改

 int func(…) const; 常函數,不可以修改所在類的成員,要修改的成員必須有mutable關鍵字

9。inline

You can see there is no function calling in assembly.

A function defined within a class definition is an inline function.

Although a function is inline, that whether to expend or not depends on compiler or its settings.一般函數裡有循環的系統認為不内聯

10.  Member-initializer-list

Const, reference and base class constructor (that has arguments) must be initialized or called in member-initializer-list

Members are initialized by order that they are defined in class definition, not by order their initializers are in member-initializer-list.

eg:

CCircle::CCircle() : PI(3.14159), m_dOrgX(0.0), m_dOrgY(0.0)

{

}

基類的初始化,按照子類定義時的順序,不時初始化清單的順序

11。 class Default functions

Constructor [do nothing]

Copy constructor [bitwise copy]

Destructor ( ~ ) [do nothing]

Assignment operator ( = ) [bitwise copy]

Address operator ( & )

Dereference operator ( * )

//當有指針是自己定義拷貝構造和指派

eg:

        if(other.m_pBuffer!=NULL) {

 m_pBuffer=new TCHAR[tcslen(other.m_pBuffer)+1];

 tcscpy(m_pBuffer,other.m_pBuffer);}

12 this 指針

Point lower;

lower.setX(20).setY(30).doubleMe() ;

因為:

     Point& setX(int x) { _x = x; return *this;}  

     Point& setY(int y) { _y = y; return *this;}  

     void doubleMe()

 {  _x *= 2;

  _y *= 2;  

 }

13。    const   static     int PI=1;類中的成員直接指派,隻有這一種情況可以,必須為int.

        double CCircle::PI = 1.0;類中的靜态成員初始化必須再類外,而且還的有類型, 如前面.

        靜态成員的初始化不能再構造函數中進行!

14.  Dynamic_cast

The dynamic_cast is used for safe casting from a pointer to a base class to a pointer to a derived class, often referred to as safe down casting. It is used when one must use the features of the derived class that are not present in the base class.

typeid            typeid( object );

The typeid operator returns a reference to a type_info object that describes `object`.

If the expression is of class type and the class contains one or more virtual member functions, then the answer may be different than the type of the expression itself. For example, if the expression is a reference to a base class, the typeid operator indicates the derived class type of the underlying object.

PTTI運作時刻類型識别允許“用指向基類的指針或引用來操作對象”的程式能夠擷取到“這些指針或引用所指的對象”的實際派生類型。

1. Dynamic_cast它允許在運作時刻進行類型轉換,進而使程式能夠在一個類層次結構中安全地轉換類型,把基類指針轉化成派生類指針,或把指向基類的左值轉換成派生類的引用,當然隻有在保證轉換能夠成功的情況下可以。

2. typeid操作符,它指出指針或引用指向的對象的實際派生類型。它在程式中可以用于擷取一個表達式是一個類類型,并且含有一個或多個虛函數成員,則答案會不同于表達式本身的類型。例如,如果表達式是一個基類的引用,則typeid會指出底層對象的派生類類型。

15. What exceptions can a function throw if it has an exception specification of the form throw()?  (6’) throw all kinds of exceptions

If  it has no exception specification?

What means the syntax: catch (…) catch all types of exceptions