从内置类型到自定义类的转换

发布于 2024-11-03 01:11:32 字数 265 浏览 3 评论 0原文

我有一个自定义类,它充当一个名为 Integer 的 int,我想告诉编译器如何自动将某些类型转换为 Integer,这样我就可以避免一遍又一遍地输入相同的内容,

someCall(Integer(1), Integer(2));

这将成为

someCall(1,2);

我用谷歌搜索的但我所能做的find 是做相反的事情,将 Integer 转换为 int 我想完成相反的任务。

I have a custom class that acts as an int called Integer, I would like to tell compiler how to convert certain types to Integer automatically so that I can avoid typing same thing over and over again,

someCall(Integer(1), Integer(2));

would become

someCall(1,2);

I've googled but all I could find is to do the oposite, casting Integer to int I would like to accomplish the opposite.

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

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

发布评论

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

评论(3

ぇ气 2024-11-10 01:11:32

编写一个采用 int 的构造函数,如下所示:

class Integer
{
   public:
       Integer(int);
};

如果类 Integer 有这个构造函数,那么你可以这样做:

void f(Integer);

f(Integer(1)); //okay 
f(1);          //this is also okay!

解释是,当你编写 f(1 ),然后会自动调用采用 int 类型的单个参数的 Integer 构造函数并动态创建一个临时变量,然后该临时变量获取传递给函数!


现在假设您想做完全相反的事情,即将 Integer 类型的对象传递给需要 int 的函数:

 void g(int); //NOTE: this takes int!

 Integer intObj(1);
 g(intObj); //passing an object of type Integer?

要使上述代码正常工作,您需要做的是,在类中定义用户定义的转换函数为:

class Integer
{
   int value;
   public:
       Integer(int);
       operator int() { return value; } //conversion function!
};

因此,当您将 Integer 类型的对象传递给采用 int 的函数时,就会调用转换函数并对象隐式转换为 int,然后作为参数传递给函数。您还可以这样做:

int i = intObj; //implicitly converts into int
                //thanks to the conversion function!   

Write a constructor that takes int, as:

class Integer
{
   public:
       Integer(int);
};

If the class Integer has this constructor, then you can do this:

void f(Integer);

f(Integer(1)); //okay 
f(1);          //this is also okay!

The explanation is that when you write f(1), then the constructor of Integer that takes a single argument of type int, is automatically called and creates a temporary on the fly and and then that temporary gets passed to the function!


Now suppose you want to do exactly the opposite, that is, passing an object of type Integer to a function takes int:

 void g(int); //NOTE: this takes int!

 Integer intObj(1);
 g(intObj); //passing an object of type Integer?

To make the above code work, all you need is, define a user-defined conversion function in the class as:

class Integer
{
   int value;
   public:
       Integer(int);
       operator int() { return value; } //conversion function!
};

So when you pass an object of type Integer to a function which takes int, then the conversion function gets invoked and the object implicity converts to int which then passes to the function as argument. You can also do this:

int i = intObj; //implicitly converts into int
                //thanks to the conversion function!   
情绪失控 2024-11-10 01:11:32

您可以在 Integer 中为要隐式转换的类型定义构造函数。不要让它们显式

You could define constructors in Integer for those types you want to implicitly convert. Do not make them explicit.

鹤舞 2024-11-10 01:11:32

纳瓦兹给出了正确答案。我只是想指出一些事情。
如果转换运算符不是 const,则无法转换 const 对象

const Integer n(5);
int i = n; // error because non-const conversion operator cannot be called

最好将转换运算符声明为

operator int() const {return value;}

Nawaz has given the correct answer. I just want to point out someting.
If the conversion operator is not const, you can't convert const objects

const Integer n(5);
int i = n; // error because non-const conversion operator cannot be called

Better declare your conversion operator as

operator int() const {return value;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文