C + + Primer Learning summary 14th Chapter operation overloading and type conversion

Source: Internet
Author: User

14th Operation overloading and type conversion


1. You can call the overloaded operator function directly .

However, if you have overloaded member functions and overloaded non-member functions for an operator such as +, you will get an error if you use the + sign directly at this point . . Because the compiler does not know which operator to invoke.

2. comma , operator logic with && operator logic or | | operator does not recommend overloading.

Since the above 3 operators themselves are evaluated in order of evaluation and short-circuit evaluation characteristics (&& and | | | have short-circuit evaluation characteristics ) . but the overloaded operator is essentially a function call , so both the evaluation order and the short-circuit evaluation feature disappear .

Notice the first one above a&& (v--) printed is the 1 and not 0. Even if you use () to enclose v--, The return value of this expression is still the original value of v .

3. Assignment (=), subscript ( []) , call (()), member access Arrow (-) The overloaded operator function must be defined as a member function of the class ( otherwise the compilation does not pass ).

4. The order of the different type parameters of the overloaded operator cannot be changed .

Because the a+int operator is overloaded, A+v is correct but v+a is wrong.

But the following "111" and s can be arbitrarily swapped order?

Because the defined non-member function operator+ (string,string), instead of operator+ (const char*, string) Therefore, string constants are automatically converted to string objects implicitly.

5. Overloaded input and output operators >> and <<.

The OS is parameter and the return value of the first two operators must be non-const references. Because stream objects are not copied and change state.

Second, for the input operator >>, the object of the class must be a non- const reference . For the output operator <<, the object of the class is proposed as a const reference.

6. Equality operator = = with unequal operator! =

In general, a class defines the = = operator, so it is common to define an unequal! = operator.

The! = operator simply calls the = = operator to work, that is, only one operator is really responsible for comparing work.

7. You can define multiple overloaded operators for a class.

8. Overloaded subscript operator []

The overloaded subscript operator [] should first return a reference , followed by a general definition of the const version and the non-const version of two overloaded functions.

Because B is a constant, we cannot assign a value to b[2.

9. Reload Increment + + decrement--operator.

First, each operator recommends overloading both the front and back two versions.

Second, the predecessor version returns the reference, and the backend version returns the value.



10. Reload * Dereference and member access operator.

Attention:

* Dereference A reference to the returned object, - the member access operator returns the address of the member.

- The fact that the member access operator gets a member never changes .



11. Class Type Conversions

Class-type conversions are defined by implicit conversion constructors (single-argument constructors) and type-conversion operators .

Implicit conversion Constructors : Other types are converted to the type of the current class.

type conversion operator : The current class object is converted to another type.

The type conversion operator has no display return type, and the invisible parameter must be defined as a member function of the class.


12. There are two types of semantic conversions

Scenario One: Assume that you are now converting type B objects to type a objects. And if there is a constructor for the B object in a, and the target object is a type conversion operator in B, then from B->a, you can call both A's conversion constructor and B's conversion operator to produce two semantics.

Scenario Two : Be careful if Class A exists with conversions between multiple built-in arithmetic types.

Scenario Three : Assume that func (Dd) and func (CC) are two overloaded functions. and the D and Class C objects can be converted with int, then the func (10) call will produce two semantics.

Conclusion : In addition to the explicit (explicit) conversion to type bool, you should avoid defining type conversion operators and non-explicit conversion constructors as much as possible.

C + + Primer Learning summary 14th Chapter operation overloading and type conversion

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.