C++ 什么是“operator int”函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3814865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is an "operator int" function?
提问by ankit
What is the "operator int" function below? What does it do?
下面的“operator int”函数是什么?它有什么作用?
class INT
{
int a;
public:
INT(int ix = 0)
{
a = ix;
}
/* Starting here: */
operator int()
{
return a;
}
/* End */
INT operator ++(int)
{
return a++;
}
};
采纳答案by John Dibling
The bolded code is a conversion operator. (AKA cast operator)
粗体代码是一个转换运算符。(又名演员操作员)
It gives you a way to convert from your custom INT
type to another type (in this case, int
) without having to call a special conversion function explicitly.
它为您提供了一种从自定义INT
类型转换为另一种类型(在本例中为int
)的方法,而无需显式调用特殊的转换函数。
For example, with the convert operator, this code will compile:
例如,使用转换运算符,此代码将编译:
INT i(1234);
int i_2 = i; // this will implicitly call INT::operator int()
Without the convert operator, the above code won't compile, and you would have to do something else to go from an INT
to an int
, such as:
如果没有 convert 运算符,上面的代码将无法编译,您必须执行其他操作才能从 an 转换INT
为 an int
,例如:
INT i(1234);
int i_2 = i.a; // this wont compile because a is private
回答by Mike Seymour
operator int()
is a conversion operator, which allows this class to be used in place of an int
. If an object of this type is used in a place where an int
(or other numerical type) is expected, then this code will be used to get a value of the correct type.
operator int()
是一个转换运算符,它允许使用此类代替int
. 如果在需要int
(或其他数字类型)的地方使用此类型的对象,则此代码将用于获取正确类型的值。
For example:
例如:
int i(1);
INT I(2); // Initialised with constructor; I.a == 2
i = I; // I is converted to an int using `operator int()`, returning 2.
回答by Chubsdad
First things first:
第一件事:
$12.3.1/1 - "A constructor declared without the function-specifier explicit specifies a conversion from the types of its parameters to the type of its class. Such a constructor is called a converting constructor."
$12.3.1/1 - “在没有函数说明符的情况下声明的构造函数明确指定了从其参数类型到其类类型的转换。这样的构造函数称为转换构造函数。”
In your example, INT is a User Defined class that has a converting constructor from 'int'.
在您的示例中,INT 是一个用户定义的类,它具有来自“int”的转换构造函数。
Therefore the following code is well-formed:
因此,以下代码格式良好:
INT i(1024); // direct initialization syntax
This means that you can get an INT object from an integer. However what does one do, if the INT object has to be converted back to an integer? Transitivity?
这意味着您可以从整数获取 INT 对象。但是,如果必须将 INT 对象转换回整数,该怎么办?传递性?
One can say that the class INT can provide a member function to return the encapsulated integer member
可以说INT类可以提供一个成员函数返回封装的整数成员
int x = i.geta();
This however is not very intuitive and is not a standardized approach. Also it is not intuitive when it comes to how built-in types work in such situations.
然而,这不是很直观,也不是标准化的方法。此外,在这种情况下,内置类型的工作方式也不直观。
int z = 0;
int y1 = z; // copy initialization or
int y2(z); // direct initialization
double d = (int )z; // explicit cast
Therefor the Standard allows for such standardization and intuitiveness of converting User Defined Types by saying:
因此,标准允许通过以下方式转换用户定义类型的标准化和直观性:
$12.3/2 - "A member function of a class X having no parameters with a name of the form [...]
operator conversion-type-id
[...]specifies a conversion from X to the type specified by the conversion-type-id. Such functions are called conversion functions. No return type can be specified. If a conversion function is a member function, the type of the conversion function (8.3.5) is “function taking no parameter returning conversion-type-id”.
$12.3/2 - “X 类的成员函数,没有参数,名称为 [...]
运算符转换类型 ID
[...] 指定从 X 到由 conversion-type-id 指定的类型的转换。此类函数称为转换函数。不能指定返回类型。如果转换函数是成员函数,则转换函数(8.3.5)的类型为“不带参数返回conversion-type-id的函数”。
This makes all of the following well-formed and retains harmony with the way built-in types workis
这使得所有的下列常见的形成和保持和谐与内建类型的工作方式是
int y1 = i; // copy initialization or
int y2(i); // direct initialization
double d = (int )i; // explicit cast
回答by John Smith
It looks like it make an INT class which behaves a little like the regular int, just that some other operators are not yet defined.
看起来它创建了一个行为有点像常规 int 的 INT 类,只是其他一些运算符尚未定义。
Is this a homework problem?
这是家庭作业问题吗?
回答by Wernight
Seems like it's a question from a classroom, so I'll invite you to check the documentation on how to create a class.
好像是课堂提问,请大家查一下创建班级的文档。
class Foo
{
public
Foo() {} // Constructor
Foo operator++ {} // Operation ++ on foo like:
// Foo foo;
// foo++;
};