User-defined conversions in C + +

Source: Internet
Author: User
Tags arithmetic bitwise operators logical operators

(1) The need for user-defined conversions:

We want to be able to add and subtract between smallint objects and other smallint objects or objects with built-in arithmetic types, and we want to support these operations by providing 6 smallint operator functions:

 class SmallInt ...{
friend operator+( const SmallInt &, int );
friend operator-( const SmallInt &, int );
friend operator-( int, const SmallInt & );
friend operator+( int, const SmallInt & );
public:
SmallInt( int ival ) : value( ival ) ...{ }
operator+( const SmallInt & );
operator- ( const SmallInt & );
// ...
private:
int value;
};

The two-member operator allows us to add and subtract two smallint objects. The friend global operator allows us to add and subtract between the smallint object and the object of the built-in arithmetic type. Only 6 operators are required because any built-in arithmetic type can be converted to match the int parameter.

If we also want to support bitwise operators, logical operators, relational operators, and compound assignment operators, the number of required operators becomes very scary. We prefer not to provide all the overloaded operators, but rather a way to automatically convert the SmallInt class object to an int object.

C + + provides a mechanism by which each class can define a set of transformations that can be applied to that type of object. For smallint, we define a conversion from the smallint object to the int type.

 class SmallInt ...{
public:
SmallInt( int ival ) : value( ival ) ...{ }
// 转换操作符
// SmallInt ==> int
operator int() ...{ return value; }
// 没有提供重载操作符
private:
int value;
};

(2) Places to be noted:

(a) The conversion function must be a member function, and its declaration cannot specify the return type and parameter table.

 operator int( SmallInt & ); // 错误: 不是成员
class SmallInt ...{
public:
int operator int(); // 错误: 返回类型
operator int( int = 0 ); // 错误参数表
// ...
};

(b) An explicit type conversion causes the invocation of a conversion function. (Be careful to set the conversion type to const when necessary, allowing only read-only access to the converted class object)

 #include "Token.h"
Token tok( "function", 78 );
// 函数型的表示法: 调用 Token::operator SmallInt()
SmallInt tokVal = SmallInt( tok );
// static_cast: 调用 Token::operator tName()
char *tokName = static_cast< char * >( tok );

(c) If the target of the conversion (double in this case) does not exactly match the type of the conversion function (the int type in this case), but the target type can be reached through a standard conversion sequence, the conversion function can still be invoked.

 extern void calc( double );
Token tok( "constant", 44 );
// 调用 tok.operator int() 吗? 是的
// int --> double 通过标准转换
calc( tok );

(d) Use constructors as conversion functions.

 extern void func( Number );
SmallInt si( 87 );
{
// 调用 Number( const SmallInt & )
func( si );
}

Of course, you can disallow the use of an implicit conversion by setting explicit before the constructor of the class.

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.