它叫 "implicit casting" 还是 "implicit conversion"?
Is it called "implicit casting" or "implicit conversion"?
尽管我看到 C++ 教程中广泛使用术语 implicit casting
来表示这样一个事实,即当您将某种类型分配给另一种类型时,类型的转换将自动(隐式)完成,但我经常听到它应该被称为 implicit conversion
而不是 implicit casting
。谁能告诉我这两者的区别?
正常应该叫隐式转换
唯一一次你可能会看到有人合理地谈论 "implicit cast" 是在谈论 class 中的强制转换运算符时。例如:
class T {
int x;
public:
T (int x) : x(x) {}
operator int() { return x; }
};
有些人称之为强制转换运算符,这是一种可以隐式调用的运算符。从 C++11 开始,您可以向其添加 explicit
:
class T {
int x;
public:
T (int x) : x(x) {}
explicit operator int() { return x; }
};
...以防止隐式调用。例如,这意味着:
T t(10);
int x = t; // works with the first version, not the second
int y = static_cast<int>(t); // works with either version
所以,如果有人是 comparing/contrasting 这两个,那么他们将第一个称为 "implicit cast operator"(或类似的东西)以将其与第二个
尽管我看到 C++ 教程中广泛使用术语 implicit casting
来表示这样一个事实,即当您将某种类型分配给另一种类型时,类型的转换将自动(隐式)完成,但我经常听到它应该被称为 implicit conversion
而不是 implicit casting
。谁能告诉我这两者的区别?
正常应该叫隐式转换
唯一一次你可能会看到有人合理地谈论 "implicit cast" 是在谈论 class 中的强制转换运算符时。例如:
class T {
int x;
public:
T (int x) : x(x) {}
operator int() { return x; }
};
有些人称之为强制转换运算符,这是一种可以隐式调用的运算符。从 C++11 开始,您可以向其添加 explicit
:
class T {
int x;
public:
T (int x) : x(x) {}
explicit operator int() { return x; }
};
...以防止隐式调用。例如,这意味着:
T t(10);
int x = t; // works with the first version, not the second
int y = static_cast<int>(t); // works with either version
所以,如果有人是 comparing/contrasting 这两个,那么他们将第一个称为 "implicit cast operator"(或类似的东西)以将其与第二个