显式类型转换运算符:()
C++ 允许使用与函数调用语法类似的语法进行显式类型转换。
语法
simple-type-name ( expression-list )
备注
后跟包含在括号中的 expression-list 的 simple-type-name 使用指定表达式构造指定类型的对象。 以下示例显示到类型 int 的显式类型转换:
int i = int( d );
以下示例显示了 Point
类。
示例
// expre_Explicit_Type_Conversion_Operator.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
class Point
{
public:
// Define default constructor.
Point() { _x = _y = 0; }
// Define another constructor.
Point( int X, int Y ) { _x = X; _y = Y; }
// Define "accessor" functions as
// reference types.
unsigned& x() { return _x; }
unsigned& y() { return _y; }
void Show() { cout << "x = " << _x << ", "
<< "y = " << _y << "\n"; }
private:
unsigned _x;
unsigned _y;
};
int main()
{
Point Point1, Point2;
// Assign Point1 the explicit conversion
// of ( 10, 10 ).
Point1 = Point( 10, 10 );
// Use x() as an l-value by assigning an explicit
// conversion of 20 to type unsigned.
Point1.x() = unsigned( 20 );
Point1.Show();
// Assign Point2 the default Point object.
Point2 = Point();
Point2.Show();
}
输出
x = 20, y = 10
x = 0, y = 0
尽管前面的示例演示了使用常量的显式类型转换,但在对对象执行转换时,此方法也同样有效。 以下代码片段对此进行了演示:
int i = 7;
float d;
d = float( i );
还可以使用“cast”语法指定显式类型转换。 使用 cast 语法重写的上一个示例是:
d = (float)i;
当从单个值转换时,强制转换和函数样式转换都有相同的结果。 但是,在函数样式语法中,可以为转换指定多个自变量。 此差异对用户定义的类型非常重要。 请考虑 Point
类及其转换:
struct Point
{
Point( short x, short y ) { _x = x; _y = y; }
...
short _x, _y;
};
...
Point pt = Point( 3, 10 );
前面使用函数样式转换的示例演示了如何将两个值(一个用于 x,另一个用于 y)转换为用户定义类型 Point
。
注意
请谨慎使用显式类型转换,因其会重写 C++ 编译器的内置类型检查。
强制转换批注必须用于到没有 simple-type-name(例如,指针或引用类型)的类型的转换。 到可与 simple-type-name 一起表示的类型的转换可以使用任何一种形式写入。
在强制转换中的类型定义是非法的。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈