智能家居引熱議 那些智能家電APP你知道嗎?

來源:互聯網
上載者:User

標籤:c++   debug   函數重載   對象   

建構函式再探
建構函式初始值列表
 Sales_data::Sales_data(const string &s, unsigned cnt, double price)    {        bookNo=s;        units_sold=cnt;        revenue=cnt*price;    }

這個建構函式和這個效果是一樣的
    Sales_data(const std::string &s, unsigned n, double p):        bookNo(s),units_sold(n),revenue(p*n){}

建構函式的初始值有時必不可少當成員是const類型或者是引用的時候就必須將其初始化,或者是類類型的時候,且該類沒有建構函式定義的時候,那麼也要將其初始化
class ConstRef{public:    ConstRef(int ii);private:    int i;    const int ci;    int &ri;};//錯誤的建構函式的初始化ConstRef::ConstRef(int ii){    //賦值    i=ii;       //正確    ci=ii;      //錯誤:不能給const賦值    ri=i;       //錯誤:ri沒被初始化}//正確的開啟檔案ConstRef::ConstRef(int ii):i(ii),ci(ii),ri(i){    }

建議:使用建構函式初始值(就是上面的正確開啟檔案) 在很多類中,初始化和賦值的區別事關底層效率問題,前者(就是上面的正確開啟檔案)直接初始化,後者(賦值錯誤開啟檔案)先初始化後賦值。


成員初始化的順序成員初始化的順序和他們在類定義中的出現順序一致。
class X{    int i;  //先被初始化    int j;  //後被初始化public:    X(int val):j(val),i(j){}};

<span style="font-weight: normal;">class Sales_data{    //為Sales_data的非成員函數所做的友元聲明    friend Sales_data add(const Sales_data&, const Sales_data&);    friend std::istream &read(std::istream&, Sales_data&);    friend std::ostream &print(std::ostream&, const Sales_data&);public:         //增加了訪問說明符    //建構函式    Sales_data() = default;    Sales_data(const std::string &s, unsigned n, double p):        bookNo(s),units_sold(n),revenue(p*n){}    Sales_data(const std::string s="11"):bookNo(s){cout<<"xxxx"<<endl;}    Sales_data(std::istream &is){read(is, *this);}    /*    Sales_data::Sales_data(const string &s, unsigned cnt, double price)    {        bookNo=s;        units_sold=cnt;        revenue=cnt*price;    }*/    //普通成員函數    std::string isbn()const {return bookNo;}    Sales_data &combine(const Sales_data&);private:    double avg_price() const    {        return units_sold ? revenue/units_sold : 0;    }    std::string bookNo;    unsigned units_sold=0;    double revenue=0.0;};//Sales_data介面的非成員組成部分的聲明Sales_data add(const Sales_data&, const Sales_data&);std::istream &read(std::istream&, Sales_data&);std::ostream &print(std::ostream&, const Sales_data&);int main(){    Sales_data next();    Sales_data last("9-999-99999-9");    return 0;}</span>

這裡面,main函數裡面一個調用的defalut一個調用的是
<span style="font-weight: normal; ">Sales_data(const std::string s="11"):bookNo(s){cout<<"xxxx"<<endl;}</span>

委託建構函式
    //建構函式    Sales_data(const std::string &s, unsigned n, double p):        bookNo(s),units_sold(n),revenue(p*n){}    //其餘建構函式都委託給另外建構函式    Sales_date():Sales_data("",0,0) {}    Sales_data(std::string s):Sales_data(s,0,0) {}    Seles_data(std::istream &is):Sales_data() {read(is, *this);}


彙總類彙總類使得使用者可以直接存取其成員,並且具有特殊的初始化文法形式。一般使用struct定義的類就是彙總類並且聲明一個類的對象的時候,初始值順序必須和聲明順序一致

字面值常量類
class Debug{public:    constexpr Debug(bool b=true):hw(b),io(b),other(b) {}    constexpr Debug(bool h, bool i, bool o):hw(h),io(i),other(o) {}    constexpr bool any() {return hw || io || other;}    void set_io(bool b) {io=b;}    void set_hw(bool b) {hw=b;}    void set_other(bool b) {other=b;}private:    bool hw;        //硬體錯誤    bool io;        //IO錯誤    bool other;     //其他錯誤};

使用了constexpr的建構函式意味著1、不能有返回值2、擁有唯一可執行檔語句返回語句。。。。綜合就是函數體應該是空的。這裡我覺得書上這樣翻譯純屬扯淡,自相矛盾,應該看英文版本的!!!可惜my English is very pool!!

PS:堅持不懈,不在多,而在精華,但是有時候會給一點糟粕,那是因為時間過得太快,不能再那些沒必要的小事上浪費時間,但是碰到自己不熟悉的,不清楚的那麼你就得好好看看一看,停下腳步認真的學一學了!!!!









相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.