• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            c++ effective心得

            1.Operator overloading

             

            一般的:  c& c::operator=(const c&){。。。。return *this;};   --------格式  運(yùn)算符的重載
               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
            區(qū)別:
             T& operator ++() //prefix ++a
             T operator++(int) //postfix a++


            一般情況下幾乎總要遵循operator=輸入和返回的都是類對(duì)象的引用的原則.
            必須返回一個(gè)對(duì)象時(shí)不要試圖返回一個(gè)引用,當(dāng)需要在返回引用和返回對(duì)象間做決定時(shí),你的職責(zé)是選擇可以完成正確功能的那個(gè)。至于怎么讓這個(gè)選擇所產(chǎn)生的代價(jià)盡可能的小,那是編譯器的生產(chǎn)商去想的事。

            2.
            const成員函數(shù)不被允許修改它所在對(duì)象的任何一個(gè)數(shù)據(jù)成員。mutable在處理“bitwise-constness限制”問題時(shí)是一個(gè)很好的方案,但它被加入到c++標(biāo)準(zhǔn)中的時(shí)間不長(zhǎng),所以有的編譯器可能還不支持它。
            explicit表示必須顯示的調(diào)用該函數(shù)。

            3.void including more times
            #findef
            #define

            #endif

            等價(jià)于
            #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
            我們可以用以下改變默認(rèn)的對(duì)齊方式。
            #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. 一般位類名_函數(shù)名_參數(shù)列表

            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函數(shù),必須被顯示的調(diào)用


            8。const in class
              4 cases in a class
             const int member;只能在初始化列表里進(jìn)行初始化
             int funct( const int nFactor,…);參數(shù)位常熟,在函數(shù)種不可被修改
             const int func(…); 返回的值位常熟不可以再被賦值和修改
             int func(…) const; 常函數(shù),不可以修改所在類的成員,要修改的成員必須有mutable關(guān)鍵字


            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.一般函數(shù)里有循環(huán)的系統(tǒng)認(rèn)為不內(nèi)聯(lián)
            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)
            {
            }
            基類的初始化,按照子類定義時(shí)的順序,不時(shí)初始化列表的順序

            11。 class Default functions
            Constructor [do nothing]
            Copy constructor [bitwise copy]
            Destructor ( ~ ) [do nothing]
            Assignment operator ( = ) [bitwise copy]
            Address operator ( & )
            Dereference operator ( * )

            //當(dāng)有指針是自己定義拷貝構(gòu)造和賦值
            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() ;
            因?yàn)椋?BR>     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;類中的靜態(tài)成員初始化必須再類外,而且還的有類型, 如前面.
                    靜態(tài)成員的初始化不能再構(gòu)造函數(shù)中進(jìn)行!

            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運(yùn)行時(shí)刻類型識(shí)別允許“用指向基類的指針或引用來操作對(duì)象”的程序能夠獲取到“這些指針或引用所指的對(duì)象”的實(shí)際派生類型。
            1. Dynamic_cast它允許在運(yùn)行時(shí)刻進(jìn)行類型轉(zhuǎn)換,從而使程序能夠在一個(gè)類層次結(jié)構(gòu)中安全地轉(zhuǎn)換類型,把基類指針轉(zhuǎn)化成派生類指針,或把指向基類的左值轉(zhuǎn)換成派生類的引用,當(dāng)然只有在保證轉(zhuǎn)換能夠成功的情況下可以。
            2. typeid操作符,它指出指針或引用指向的對(duì)象的實(shí)際派生類型。它在程序中可以用于獲取一個(gè)表達(dá)式是一個(gè)類類型,并且含有一個(gè)或多個(gè)虛函數(shù)成員,則答案會(huì)不同于表達(dá)式本身的類型。例如,如果表達(dá)式是一個(gè)基類的引用,則typeid會(huì)指出底層對(duì)象的派生類類型。


            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

            posted on 2005-11-09 12:56 夢(mèng)在天涯 閱讀(1864) 評(píng)論(2)  編輯 收藏 引用 所屬分類: CPlusPlus

            評(píng)論

            # re: c++ effective心得 2006-08-17 12:26 keyws

            T& operator ++() //prefix ++a
            T operator++(int) //postfix a++
            怎么解釋這個(gè)?即編譯器如何決定調(diào)用哪個(gè)?

              回復(fù)  更多評(píng)論   

            # re: c++ effective心得 2008-10-14 14:33 ggz

            @keyws
            一個(gè)是++a 一個(gè)是a++  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1811215
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            久久噜噜久久久精品66| 97久久香蕉国产线看观看| 伊人情人综合成人久久网小说| 亚洲日本va午夜中文字幕久久| 久久影视国产亚洲| 久久精品国产精品亚洲毛片| 久久精品中文字幕久久| 中文字幕精品久久久久人妻| 伊人久久大香线蕉av一区| 99国内精品久久久久久久| 精品久久久久久无码不卡| 九九99精品久久久久久| 久久人人爽人人人人片av| 日本精品久久久久中文字幕| 久久精品国产欧美日韩99热| 久久久久国产一级毛片高清版| 久久久久久国产a免费观看黄色大片| 国产午夜精品久久久久免费视| 欧美亚洲另类久久综合婷婷| 日本精品久久久久中文字幕8| 一本一本久久aa综合精品| 香蕉aa三级久久毛片| 久久99精品久久久久久齐齐| 国产精品久久久久久福利漫画 | 伊人久久综合无码成人网| 精品久久久久久无码人妻热| 99久久婷婷国产综合亚洲| 久久久无码精品亚洲日韩蜜臀浪潮| 国产99久久久国产精品~~牛| 97久久久久人妻精品专区 | 18岁日韩内射颜射午夜久久成人| 国产精品久久久久久久久久影院 | 免费观看久久精彩视频| 色8久久人人97超碰香蕉987| 精品熟女少妇AV免费久久| 久久久久99这里有精品10 | 久久精品中文字幕第23页| 国产精品成人久久久久久久| 久久国产乱子精品免费女| 精品国产一区二区三区久久| 999久久久免费精品国产|