C++ Primer 5th notes (chap 14 overload operation and type conversion) class type conversion

1. Definition

Type conversion operator: (Since a class type can be implicitly converted from an argument type object, then) the value of a class type can also be converted to other types.
Form: operator type() const
type represents a certain type, type conversion Operators can be defined for any type (except void), as long as the type can be used as the return type of the function

User-defined class type conversion = conversion constructor + type conversion operator

eg.

class SmallInt{
    
    
public:
	SmallInt(int i= 0):val(i)
	{
    
    
		if(i < 0 || i > 255)
			throw std::out_of_range("Bad SmallInt value");
	}
	operator int() const{
    
    return val;}
private:	
	std::size_t val;
}

The constructor converts the value of arithmetic type to SmallInt, and the type conversion operator converts SmallInt object to int.
Implicit user-defined type conversion can be placed before or after a standard (built-in) type conversion

SmallInt si;
si = 4;	//首先将4隐式转换成SmallInt,然后调用SmallInt::operator=  
int n = si + 3;//首先将si隐式地转换成int,然后执行整数加法
cout << n << endl;//调用lambda函数对象 

2. Design principles

  • The type conversion operator is a special member function of the class, which is responsible for converting the value of a class type to other types
  • Conversion to array or function type is not allowed, but conversion to pointer (including: array pointer, function pointer) or reference type is allowed.
  • No return type, no formal parameters
  • Generally, the content of the object to be converted should not be changed. Therefore, the type conversion operator is generally defined as a const member
  • Avoid excessive use of type conversion functions.

eg.

class SmallInt{
    
    
public:
    //编译器不会自动执行这一类型转换

   int operator int() const;//error:指定了返回类型
   operator int(int = 0) const;//error:指定了形参 
}

3. Implicit type conversion

3.1 Implicitly may produce unexpected results

eg.

//使用 istream 的 bool 类型转换运算符将 cin 转换成 bool, 可能会变成1或0左移42位
int i = 42;
cin << i; 

3.2 In order to prevent implicit conversion, use explicit to specify an explicit type conversion operator, and then you can use explicit coercion

class SmallInt{
    
    
public:
    //编译器不会自动执行这一类型转换
    explicit operator int() const {
    
    return val;}
}

//显式地请求类型转换
SmallInt si=3;
int n = si + 3;//error
static_cast<int>(si)+3;//
  • Note: The type conversion to bool is usually used in the condition part, so operator bool is generally defined as explicit.
  • Under the new C++ 11 standard, the IO library defines an explicit type conversion to operator bool
while(cin >> value)

The input operator is responsible for reading the data into value and returning cin. In order to evaluate the condition, cin is implicitly converted by the istream operator bool type conversion function. If the condition status of cin is good, the function returns true, otherwise Return false.

3.3. When the expression appears in the following positions, the explicit type conversion is performed implicitly:

  • The conditional part of if, while, do statements.
  • The conditional expression at the head of the for statement.
  • Logical NOT (!) operator, logical AND (&&) operator, logical OR (||) operator.
  • The conditional expression of the conditional operator (?: ).

[Quote]

[1] Code classTypeExchange.h

Guess you like

Origin blog.csdn.net/thefist11cc/article/details/114208885