C ++ heavy-load operations, type conversion, and heavy-load operations

Source: Internet
Author: User

C ++ heavy-load operations, type conversion, and heavy-load operations

1. Basic Concept 1.1 Basic Points


1.2 call method of overloaded Operator Functions

1) Non-member functions call operator + as the non-member functions of the object data1 and data2

Data1 + data2; ==> operator + (data1, data2 );

2) call operator + as a member function of the object data1

Data1 + data2 ==> data1.operator + (data2 );

1.3 operators that should not be overloaded

The overloaded operator is essentially a function call.

1) The order in which operators are evaluated cannot be retained, for example, &, |, &, |.

2) the short-circuit evaluation attribute of the operator cannot be retained.

1.4 use the same meaning as the built-in type


1.5 select members and non-members

  • =, [], (),-> Must be a member
  • Compound assignment is generally a member.
  • Changes the object State or is closely related to a given object. It is usually a member, for example, ++, --, * unreference.
  • Symmetric operators, such as arithmetic, equality, relations, and bits, are generally non-member.
1) if an object contains a mixed expression, the operator must be defined as a non-member. 2) When defined as a member function, the operator object on the left must be an object of the class to which the operator belongs.

2. Input and Output

The input and output operators must be non-member functions, generally youyuan.

2.1 <

ostream & operator<<(ostream &os, const Object &item ){os<< item.data1<< " "<< item.data2;return os;}

2.2>

Istream & operator> (isteam & is, Object & item) {double p; is> item. data1> item. data2> p; if (is) {item. data2 = item. data2 * p;} else {item = Object (); // input failed: the Object is granted the default status} return is ;}

3 arithmetic and relationship

4 assignment

Copy assignment, move assignment, list assignment, compound assignment

The value assignment operator must be a class member, and the composite value assignment is also a member. Both classes return references to objects on the left.

5 subscript

6. Increment and decrease

7. member access

<Span style = "font-size: 14px;"> class StrPtr {public: std: string & operator * () const {auto p = check (curr, "dereference past end"); return (* p) [curr];} std: string * operator-> () const {return & this-> operator *();} // other content} </span>



8. function call

8.1 lambda is a function object
8.2 standard library function objects
8.3 callable objects and functions

  • Callable objects in c ++: functions, function pointers, lambda expressions, and objects created by bind. Classes of function call operators are reloaded.
  • Callable objects also have their own types, and different types may have the same calling form
  • Standard Library function Type ---> same call form type
  • The name of the overloaded function cannot be directly stored into the function-type object. solution 1: Use the function pointer; 2. lambda


9 type conversion Operators

Add explicit to display conversion. Note that the following conditions show that the conversion will automatically become an implicit conversion example: IO class istream, ostream9.1 avoid type conversion with Ambiguity

9.2 Function matching and overload Operators

If we provide type conversion for the same class that converts the object to the arithmetic type, and also provides overload operators, we will encounter the ambiguity between the overload operators and the built-in operators.


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.