C ++ heavy-load operations and type conversion

Source: Internet
Author: User

C ++ heavy-load operations and type conversion


1. Basic Concept 1.1 Basic Points


The implicit this pointer operator function must have at least one class type parameter overload operator with the same priority as the built-in operator ::,.*,.,? : Reload not allowed



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 assigned a composite value to a Member. Generally, a member changes the object State or is closely related to a given object, usually a member. for example, ++, --, * calls symmetric operators, such as arithmetic, equality, relationship, and bit, which 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;}




The first parameter is a very large number of ostream references. The second parameter constant reference returns the ostream type parameter to reduce formatting operations, such as line feed.




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 ;}
The first parameter is a very large number of istream parameters the second parameter is a very large number of objects reference error detection returns a given Stream Object Reference


3 arithmetic and relationship


Constant reference returns a new object that is generally defined as a composite operation that is used to implement this operation. = and! = Usually appears in pairs.


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


Must provide the extraordinary version and constant version for Members


6. Increment and decrease


The frontend and backend pairs are used to return the object reference. The backend version records the object status and then returns the recorded object.


7. member access


Constants this * and-> appear in pairs * return &,-> return pointer * first checks whether the range arrow operator must return a pointer or an object that defines the arrow Operator
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}


8. function call


An object that defines the call operator. A function object can contain State data. 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. Different types may have the same calling form. Standard Library function Type ---> the names of the same calling form-type overloaded functions cannot be directly stored into function-type objects. solution 1: Use the function pointer; 2, lambda



9 type conversion Operators


It must be a member function with no return value. A non-parameter is usually of the same type after const this conversion as the function return type, that is, it cannot be an array or function type, however, their pointers can be implicitly converted. Generally, explicit conversions are not converted into explicit conversions. Note that the conversions will be automatically converted to implicit conversions if, while, do's condition part for's condition part is not logical, or, and (!, |, &) Example of conditional expressions for conditional operators: IO class istream, ostream9.1 avoid type conversion with ambiguity. Real Parameter Matching and the same type conversion the conversion target is a built-in type multiple type conversion heavy-duty function and conversion constructor heavy-duty function and user-defined type conversion


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.



Related Article

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.