Problems with heavy-duty type conversion Operators

Source: Internet
Author: User

Today I have a question about this Code:

Operator double ()

{

Return static_cast <double> (SUM)

/Static_cast <double> (Num );

}

I think it should be like this:

Double operator double ()

{

Return static_cast <double> (SUM)

/Static_cast <double> (Num );

}

So I posted a post on csdn and asked, after being instructed by zhangweiit, mstscsi, djjlove_2008, and taodm (Thank you very much), I understood that this is the c ++ syntax rule, I don't know. So I opened the 4-edition p454 of C ++ primer, which has been a long-overdue issue, and sorted it out (excerpted). I hope to master this knowledge point and the people who see this article will also master this knowledge point.

Conversion Operator

Conversion operator is a special class member function. It defines the conversion of class type values to other type values. The conversion operator declares in the class definition body and follows the conversion target type after the reserved word OPERATOR:

Class smallint

{

Public:

Smallint (INT I = 0); Val (I)

{

If (I <0 | I> 255)

{

Throw STD: out_of_range ("Bad smallint intializer ");

}

}

Operator int () const

{

Return val;

}

PRIVATE:

STD: size_t val;

};

The conversion function adopts the following general form:

Operator type ();

Here, type indicates the built-in type name, class type name, or name defined by a type alias. Returns any type (except void) that can be used as a function ). Generally, it is not allowed to be converted to an array or function type. It is acceptable to convert to the pointer type (data and function pointer) and reference type.

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

All of the following statements are incorrect:

Operator int (small Int &); // error: nonmember

Class smallint

{

Public:

Int operator int (); // error: return list

Operator int (Int = 0); // error: parameter list

//...

};

Although the conversion function cannot specify the return type, each conversion function must display a specified value. For example, operator int returns an int value. If operator sales_item is defined, it returns a sales_item object, and so on.

Best Practice: The conversion function should not change the converted object. Therefore, the conversion operator should generally be defined as a const member.

1. Use class type conversion

As long as there is a conversion, the compiler will automatically call it where built-in conversion can be used.

In the expression:

Smallint Si;

Double dval;

SI> = dval // Si converted to int and then convert to bool

In the condition:

If (SI) // Si convert to int and then convert to bool

Pass real parameters to the function or return values from the function:

Int calc (INT );

Smallint Si;

Int I = calc (SI); // convert Si to int and call calc

Operands as overload operators:

// Convert Si to int then call <on the int Value

Cout <Si <Endl;

In display type conversion:

Int ival;

Smallint Si = 3.54;

// Instruct compiler to cast Si to int

Ival = static_cast <int> (SI) + 3;

2. class type conversion and standard conversion

When using the conversion function, the converted type does not have to match exactly with the required type. If necessary, follow the standard conversion after class type conversion to obtain the desired type. For example, in the comparison between a smallint object and a double value:

Smallint Si;

Double dval;

SI> = dval // Si converted to int and then convert to double.

First, convert Si from the smallint object to the int value, and then convert the int value to the double value.

3. Only one class type conversion can be applied.

Annotation: the class type is converted to another class type. If you need to convert multiple class types, the code will fail.

For example, assume there is a class integral which can be converted to smallint but cannot be converted to int:

// Class to hold unsigned integral values

Class Integral

{

Public:

Integral (INT I = 0): Val (I)

{

}

Operator smallint () const

{

Return Val % 256;

}

PRIVATE:

STD: size_t val;

};

You can use integral where smallint is needed, but you cannot use integral where int is needed:

Int calc (INT );

Integral intval;

Smallint Si (intval); // OK: Convert intval to small int and copy to Si

Int I = calc (SI); // OK: Convert Si to int and call calc

Int J = calc (intval); // error: On convertion to int from integral

Use smallint to copy the constructor when creating Si. Call the integral conversion operator to generate a temporary smallint value and convert the int_val object to the smallint object. Then (merged) the copy constructor uses this object value to initialize Si.

The first calc call is also correct: the real parameter Si is automatically converted to int, and then the int value is passed to the function.

The second calc call is incorrect: there is no direct conversion from integral to int. Getting an int from intergral requires two class type conversions: first from intergral to smallint, and then from smallint to int. However, the language only allows class type conversion, so this call fails.

4. standard conversion can be placed before class type conversion

When you use a constructor to perform implicit conversion, the type of the constructor does not have to match the provided type. For example, the following code calls the constructor smallint (INT) defined in the smallint class to convert sobj to the smallint type:

Void calc (smallint );

Short sobj;

// Sobj prometed from short to int

// That int converted to smallint through the smallint (INT) constructor

Calc (sobj );

If necessary, you can apply a standard conversion sequence to real parameters before calling the constructor to perform class type conversion. To call the function calc (), apply standard conversion to convert dobj from double type to int type, then call the constructor smallint (INT) to convert the Conversion Result to smallint type.

 

 

End of this Article

Reprinted to indicate the source. Thank you.

2010-08-19

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.