Checkoutrecord related to Operator Overloading in C ++ Primer

Source: Internet
Author: User

There is a slightly special container member that defines a series of operators from input to output, to subscript, etc.

Pe14.7

// For the output operator at the top of the following class, # include "head. H "class checkoutrecord {public: checkoutrecord (STD: vector <STD: pair <STD: String, STD: String> *> wait_list): book_id (0.0 ), title ("hell"), date_borrowed (1988), date_due (1999), borrower (STD: make_pair ("Hu", "QW"), wait_list (wait_list) {} checkoutrecord (): book_id (0.0), title ("hell"), date_borrowed (1988), date_due (1999), borrower (STD: make_pair ("Hu ", "QW") {} friend STD: OS Tream & operator <(STD: ostream &, const checkoutrecord &); typedef unsigned date; private: Double book_id; STD: String title; // Date date_borrowed; // is the date standard library? First, define a date date_due; // STD: pair <STD: String, STD: String> borrower; // use pair to store the name of a single borrower STD :: vector <STD: pair <STD: String, STD: String> *> wait_list; // pointer of vector storage (pointing to each queue)}; STD :: ostream & operator <(STD: ostream & out, const checkoutrecord & check) {out <check. book_id <"\ t" <check. title <"\ t" <check. date_borrowed <"\ t" <check. date_due <"\ t" <check. borrower. first <"\ t" <check. borrower. s Econd; For (STD: vector <STD: pair <STD: String, STD: String> * >:: const_iterator iter = check. wait_list.begin (); iter! = Check. wait_list.end (); ITER ++) {// out of the cyclic output queue list <STD: Endl <"" <(ITER-check. wait_list.begin () + 1 <"queued by:" <(* ITER)-> first <"\ t" <(* ITER)-> second ;} return out;} int main () {// declare and define a queue list STD: vector <STD: pair <STD: String, STD: String> *> wait_list; STD: String first_name = "holy"; STD: String second_name = "God"; STD: pair <STD: String, STD: String> name2 = STD :: make_pair ("Zhang", "Lixin"); wait_list.push_back (& name2); // the other three have warning, because the Temporary Variable wait_list.push_back (& STD: make_pair (first_name, second_name); wait_list.push_back (& STD: make_pair (first_name, first_name); wait_list.push_back (& STD: make_pair (second_name, first_name )); // Why does the output not follow the format? It has nothing to do with the location. Put it all in the same way as checkoutrecord check1 (wait_list); STD: cout <check1 <STD: Endl ;}

Pe14_11

// Input operator, which requires error handling. The last member is definitely not an in STD: istream & operator> (STD: istream & in, checkoutrecord & check) {In> check. book_id> check. title> check. date_borrowed> check. date_due> check. borrower. first> check. borrower. second; If (! In) Check = checkoutrecord (); Return in ;}

Pe14_17

// Define the subscript operator. The subscript operator must consider the return value as the left value. If the value is assigned, check1 [1]-> first = "ASD ";Therefore, a non-const

STD: pair <STD: String, STD: String> * checkoutrecord: operator [] (const size_t index) {return wait_list [Index];} const STD :: pair <STD: String, STD: String> * checkoutrecord: operator [] (const size_t index) const {// The returned data is pointer, so it doesn't matter if it is const, you can change the queue content pairreturn wait_list [Index];} int main () {STD :: cout <check1 [1]-> first <"\ t" <check1 [1]-> second <STD: Endl ;}

Pe14_18 // advantage, easy to operate, disadvantage, the class has many members, and the subscript is only the pair pointer of the queuing list, it is not easy to understand

Another disadvantage is that no subscript cross-border check is performed. In fact, wait_list.size () is used to compare

Pe14_19 // propose another method to define this operation: Use a function to define a getqueuemember () that is exactly the same as the subscript operator. Only the names are different, however, this solves the problem that is hard to understand.

Otherwise, getqueuememberfirst, getqueuemembersecond (),

You can either concatenate a string in the implementation and return the string:

std::stringCheckoutRecord::getQueueMember(const size_t index){return wait_list[index]->first + "\t" + wait_list[index]->second;}

However, this is encapsulated and cannot be changed when the left value is changed.

The const version must be available. Otherwise, getqueuemember (1) = "ASD"; can be executed, but it has no effect and is misleading.

Const STD: string & checkoutrecord: getqueuemember const (const size_t index) const {// This is alsoNoThe returned result is a reference to the temporary string, because it is a overload (the overload caused by const ),Do not stopReturn wait_list [Index]-> first + "\ t" + wait_list [Index]-> second ;}

Const STD: stringcheckoutrecord: getqueuemember (const size_t index) const {// Define only one of them. Do not reload them. This is encapsulation. It is not a subscript operator and cannot be changed.Return wait_list [Index]-> first + "\ t" + wait_list [Index]-> second;} pe14_19.cpp: 86: 29: Error: Passing 'const STD :: string 'as' argument of 'std: basic_string <_ chart, _ traits, _ alloc> & STD: basic_string <_ chart, _ traits, _ alloc> :: operator = (const _ chart *) [with _ chart = char, _ traits = STD: char_traits <char>, _ alloc = STD: Allocator <char>, STD :: basic_string <_ chart, _ traits, _ alloc> = STD: basic_string <char>] 'discards qualifiers

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.