什么是“operator int”?功能?

发布于 2024-09-25 10:12:30 字数 275 浏览 5 评论 0原文

下面的“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++;
   }
};

What is the "operator int" function below? What does it do?

class INT
{
   int a;

public:
   INT(int ix = 0)
   {
      a = ix;
   }

   /* Starting here: */
   operator int()
   {
      return a;
   }
   /* End */

   INT operator ++(int)
   {
      return a++;
   }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里寻她 2024-10-02 10:12:30

粗体代码是转换运算符。 (又名强制转换运算符

它为您提供了一种从自定义 INT 类型转换为另一种类型(在本例中为 int)的方法,而无需显式调用特殊的转换函数。

例如,使用转换运算符,此代码将编译:

INT i(1234);
int i_2 = i; // this will implicitly call INT::operator int()

如果没有转换运算符,上述代码将无法编译,并且您必须执行其他操作才能从 INT 转换为 int,如:

INT i(1234);
int i_2 = i.a;  // this wont compile because a is private

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.

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:

INT i(1234);
int i_2 = i.a;  // this wont compile because a is private
南烟 2024-10-02 10:12:30

operator int() 是一个转换运算符,它允许使用此类来代替 int。如果在需要 int (或其他数字类型)的地方使用此类型的对象,则此代码将用于获取正确类型的值。

例如:

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.

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.

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.
妞丶爷亲个 2024-10-02 10:12:30

首先要做的事情是:

$12.3.1/1 - “声明了一个构造函数
没有函数说明符
显式指定从
其参数的类型
其类的类型。这样的构造函数
称为转换构造函数。”

在您的示例中,INT 是一个用户定义的类,具有来自“int”的转换构造函数。

因此,以下代码格式良好:

INT i(1024);     // direct initialization syntax

这意味着您可以从整数获取 INT 对象。但是如果必须将 INT 对象转换回整数,该怎么办?

可以说 INT 类可以提供一个成员函数来返回封装的整数成员,

int x = i.geta();

但这不是很直观,也不是一种标准化方法。此外,当谈到内置类型在这种情况下如何工作时,它是不直观的,

int z = 0;
int y1 = z; // copy initialization or
int y2(z);  // direct initialization
double d = (int )z; // explicit cast

因此标准允许通过以下方式转换用户定义类型的标准化和直观性:

$12.3/2 - “一个成员函数
类 X 没有带有 a 的参数
表单名称 [...]

运算符转换类型 ID

[...]指定从 X 到
指定的类型
转换类型 ID。此类函数是
称为转换函数。不予退货
可以指定类型。如果一个转换
函数是一个成员函数,
转换函数的类型
(8.3.5) 是“函数不取
参数返回
转换类型 ID”。

这使得以下所有内容都格式良好,并且与内置类型的工作方式保持和谐

int y1 = i; // copy initialization or
int y2(i);  // direct initialization
double d = (int )i; // explicit cast

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."

In your example, INT is a User Defined class that has a converting constructor from '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?

One can say that the class INT can provide a member function to return the encapsulated integer member

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”.

This makes all of the following well-formed and retains harmony with the way built-in types work is

int y1 = i; // copy initialization or
int y2(i);  // direct initialization
double d = (int )i; // explicit cast
地狱即天堂 2024-10-02 10:12:30

看起来它创建了一个 INT 类,其行为有点像常规 int,只是尚未定义一些其他运算符。

这是家庭作业的问题吗?

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.

Is this a homework problem?

星光不落少年眉 2024-10-02 10:12:30

看起来这是一个课堂问题,所以我会邀请您查看有关如何创建课程的文档。

class Foo
{
public
    Foo() {} // Constructor

    Foo operator++ {} // Operation ++ on foo like:
    // Foo foo;
    // foo++;
};

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++;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文