转换运算符如何返回值? [英] How does conversion operator return a value?

查看:202
本文介绍了转换运算符如何返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于类A,整数转换操作符看起来像:

  operator int 't指定任何返回类型
{
return intValue;
}

上述函数如何在返回值类型出现时返回一个值不指定?它似乎没有返回任何东西,但我知道它不是 void



c> 运算符T() c $ c>总是 T



它不使用标准函数原型语法 T foo(),因为2个函数(例如 int foo()冲突double foo() )。如果使用此语法,那么您只能定义1个转换运算符重载,这是不希望的。


For a class A, an integer conversion operator would look something like;

operator int() //Here we don't specify any return type
{
    return intValue;
}

How is the above function able to return a value when its return value type appears not to be specified? It doesn't appear to return "anything", but I know it's not void.

How is this meaningful when a return type is not specified?

解决方案

The return type of operator T() is always T. It's a special case of C++.

It does not use standard function prototype syntax T foo() because 2 functions with the same name differing only by the return type cannot coexist (e.g. int foo() conflicts with double foo()). If this syntax is used then you can only define 1 conversion operator overload, which is undesirable.

这篇关于转换运算符如何返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆