Smart Home Heat Discussion those smart home appliances app you know what?

Source: Internet
Author: User

A further exploration of the constructor function
Constructor Initial Value List
Sales_data::sales_data (const string &s, unsigned cnt, double price)    {        bookno=s;        units_sold=cnt;        Revenue=cnt*price;    }

This constructor is the same as the effect.
    Sales_data (const std::string &s, unsigned n, double p):        Bookno (s), Units_sold (n), Revenue (P*n) {}

The initial value of a constructor is sometimes necessary to initialize a member when it is a const type or a reference, or a class type, and the class does not have a constructor definition.
Class Constref{public:    constref (int ii);P rivate:    int i;    const int CI;    int &ri;};/ /Error constructor initialization constref::constref (int II) {    //assignment    i=ii;       Correct    ci=ii;      Error: Cannot assign value ri=i to const    ;       Error: RI not initialized}//correct open mode constref::constref (int II): I (ii), CI (ii), RI (i) {    }

Recommendation: Use the constructor initial value (that is, the correct opening method above) in many classes, the difference between initialization and assignment is related to the underlying efficiency problem, the former ( that is, the correct opening method above) is directly initialized, the latter (the assignment error open mode) is initialized first and then assigned value.


The order in which member initializations are initialized is consistent with the order in which they appear in the class definition.
Class x{    int i;  Int J is initialized first    ;  After being initialized public:    X (int val): J (Val), I (j) {}};

<span style= "Font-weight:normal;" >class sales_data{//friend Sales_data add for sales_data non-member function (const sales_data&, const sales_data&)    ;    Friend Std::istream &read (std::istream&, sales_data&); Friend Std::ostream &print (std::ostream&, const sales_data&);p Ublic://Added Access Descriptor//constructor Sales_dat    A () = default; Sales_data (const std::string &s, unsigned n, double p): Bookno (s), Units_sold (n), Revenue (P*n) {} sales_data (CO    NST std::string s= "one"): Bookno (s) {cout<< "xxxx" &LT;&LT;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;    }*///Ordinary member function std::string ISBN () const {return bookno;} Sales_data &combine (const sales_data&);p rivate:double avg_price () const {return units_sold? revenu    e/units_sold:0; } std::string BooknO    unsigned units_sold=0; Double revenue=0.0;};/ Declaration of non-member components of the/sales_data interface sales_data Add (const sales_data&, const sales_data&); Std::istream &read (std:: Istream&amp, 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>

In this, the main function inside a call to the defalut of a call is
<span style= "Font-weight:normal; ">sales_data (const std::string s="): Bookno (s) {cout<< "xxxx" <<ENDL;} </span>

Delegate constructors
    Constructor    sales_data (const std::string &s, unsigned n, double p):        Bookno (s), Units_sold (n), Revenue (P*n) {}    //The remaining constructors are delegated to another constructor    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);}


Aggregate class aggregation classes enable users to access their members directly and have a special initialization syntax form. A class that is typically defined by a struct is an aggregate class and declares an object of a class, the initial value order must be the same as the declaration order

Literal literal constant class
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 No () {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;        Hardware error    bool IO;        IO error    bool other;     Other errors};

A constructor that uses constexpr means 1, cannot have a return value of 2, and has a uniquely executable statement to return .... Synthesis is that the function body should be empty. Here I think that the translation of the book is purely nonsense, self-contradictory, should look at the English version of!!! Unfortunately my 中文版 is very pool!!

PS: Perseverance, not much, but in the essence, but sometimes give a little dross, that is because time too fast, can not be those unnecessary trivial waste of time, but encountered their unfamiliar, not clear then you have to take a good look at, stop the steps to learn a serious!!!!









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.