22. c ++ primer 4th notes, to class type conversion from class type

Source: Internet
Author: User

1,Conversion is divided into two types: class type conversion and class conversion.. Conversions to class types: conversion constructor; Conversion from class types: conversion operators.

2 Common 16 Operators: 5 Arithmetic Operators ( + , - , * , / , % ) And its corresponding compound value assignment operator, 4 Relational operators ( < , <= , > , > = ), And equal operators ( = , ! = ).

Example

 
Class smallint {public: smallint (INT I = 0): Val (I ){//...} operator int () const {return val;} // conversion operator function, which is converted from the smallint class type to the int type. PRIVATE: STD: size_t val;}; smallint Si; double dval; SI> = dval // Si converted to int and then convert to doubleif (SI) // Si converted to int and then convert to bool

General Form:Operator type ();

TypeIndicates the built-in type name, class type name, or name defined by the Type alias. For any type that can be returned as a function(BesidesVoidBesides)Can define conversion functions. Generally, conversion to an array or function type (data or function pointer) or reference is not allowed.

The conversion operator function must be a member function, and the return type cannot be specified. The parameter table must be empty.

Class type conversion cannot be passed(Both are custom conversions, rather than built-in default conversions.), SuchA-> B, B-> C,But we cannotCTransfer of type parametersAType parameter.

Example

 
Class smallint {public: smallint (INT I = 0): Val (I) // (conversion) constructor {//...} operator int () const {return val;} // conversion operator function, which is converted from the smallint class type to the int type. PRIVATE: STD: size_t val;}; class integral {public: Integral (INT I = 0): Val (I) {} operator smallint () const {return Val % 256;} PRIVATE: STD: size_t val;}; int calc (INT); integral intval; smallint Si (intval); // OK: convert intval to smallint and copy to siint I = calc (SI); // OK: Convert Si to int and call calcint J = calc (intval); // error: no conversion to int from integral

3Full-match conversion is better than other conversions that require standard conversion.

4, The ambiguity of conversion

Ambiguity

# Include "iostream" # include "vector" # include "algorithm" # include "string" # include "functional" using namespace STD; class integral; Class smallint {public: smallint (Integral ){}//...}; class integral {public: Operator smallint () const {}//...}; void compute (smallint); int main () {integral int_val; compute (int_val); // error: ambiguous}

The best way to avoid ambiguity is to avoid writing classes that provide mutual implicit conversion. There is only one way to convert one type to another.

You can explicitly force remove ambiguity.

Example

# Include "iostream" # include "vector" # include "algorithm" # include "string" # include "functional" using namespace STD; Class smallint {public: // conversions to int or double from smallint // usually it is unwise to define conversions to multiple arithmetic typesoperator int () const {return val;} operator double () const {return val ;}//... PRIVATE: STD: size_t val;}; void compute (INT); void compute (double); void compute (Long Double); int main () {smallint Si; // compute (SI); // error: ambiguous // compute (static_cast <int> (SI); // OK, call compute (INT )}

When calling overload functions, you need to use constructor or forced type conversion to convert real parameters. This is a poor design.

5To determine the overloading of operators, follow these three steps:

1) Select a candidate function.

2) Select a viable function, including identifying the potential conversion sequence of each real parameter.

3) Select the best matching function.

6, Several experiences

1) Do not define conversion classes, that is, if the classFooAccept ClassBarObject constructor.BarDefine to typeFoo.

2) Avoid conversion to built-in arithmetic types. Specifically, if the conversion to the arithmetic type is defined

ODo not define an overloaded version that accepts Arithmetic Operators. If you need to use these operators, the conversion operators will convert the objects of the type you define, and then you can use the built-in operators.

ODo not define conversions to more than one arithmetic type. Provides standard conversions to other arithmetic types.

The simplest rule is: For those "obviously correct", avoid defining conversion functions and limiting non-explicit constructors.

Reference

[1]Http://blog.163.com/zhoumhan_0351/blog/static/3995422720103182106992/

[2]Http://blog.163.com/zhoumhan_0351/blog/static/3995422720100284731826/

[3]Http://blog.163.com/zhoumhan_0351/blog/static/3995422720104931348260/

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.