是否可以在对象上定义自定义铸造成为方法调用的结果?
是否可以在对象上定义自定义铸造,以使对象铸造为方法调用结果?
class Foo {
public:
...
int method() { return 3; }
};
Foo foo;
int bar = foo + 7; // 10
Is it possible to define a custom cast on an object so that the objects casts to the result of a method call?
class Foo {
public:
...
int method() { return 3; }
};
Foo foo;
int bar = foo + 7; // 10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以提供转换函数如下所示:
方法1
直接在转换功能内使用
int
而无需使用方法
。demo
或
方法2
使用
方法
返回int
int 从转换功能内部。You could provide a conversion function as shown below:
Method 1
Directly return an
int
without usingmethod
inside the conversion function.Demo
Or
Method 2
Use
method
to return anint
from inside the conversion function.AGH。我过度想到了。
Agh. I was overthinking it.