我们可以在返回类型上重载 [英] Can we overload on return types
问题描述
嘿所有
这是一个基本问题吗?我在一本书中读到了Thinking in C ++。它说
"仅仅在返回值上重载有点过于微妙,因此在C ++中是不允许的。虽然我在VC ++编译器上尝试重载,但是我可以在返回时重载
类型。
请评论
Pankaj
Panks写道:
这是一个基本问题吗?我在一本书中读到了Thinking in C ++。它说
"仅仅在返回值上重载有点过于微妙,因此在C ++中是不允许的。在第323页上。虽然我在
尝试在VC ++编译器上重载,但是在返回
类型时我能够重载。
请举例说明您的成功。或者你错误地将b
省略了不是在是之间和能够?
V
-
请在回复时删除资金''A'邮件
我没有回复最热门的回复,请不要问
Pankaj,
>
我不认为C ++语言或其符合任何符合条件的编译器允许这样做。 TICPP正确地提到了这一点。但是,使用模板可以获得类似的行为。
。例如。
模板< class T>
T overloaded_on_return_type()
{
T var;
cout<< " \\\
" << var;
return var;
}
用于调用模板函数的类型必须是
内在类型或它应该是一些UDT,它有插入操作符
为它重载(至少在这个例子中)。
调用这个功能就像
''overloaded_on_return_type< int>();''。
问候,
Harish Sharma
sh ******* ***@gmail.com 写道:
>
调用此函数就像
一样简单
''overloaded_on_return_type< int>();''。
尽管有名称,但这并没有超载。
Hey All
Its one of basic questions? i read in a book "Thinking in C++" it says
" Overloading solely on return value is a bit
too subtle, and thus isn''t allowed in C++." on page 323. While i
tried overloading on VC++ compiler i was able to overload on return
types.
Please comment
Pankaj
Panks wrote:Its one of basic questions? i read in a book "Thinking in C++" it says
" Overloading solely on return value is a bit
too subtle, and thus isn''t allowed in C++." on page 323. While i
tried overloading on VC++ compiler i was able to overload on return
types.Please give us the example of your success. Or did you mistakenly
omit the "not" between "was" and "able"?
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Pankaj,
I don''t think that is allowed by C++ language or by any of its
conforming compilers. TICPP mentions this correctly. However, there is
a way by which you can get similar behaviour, by using templates. eg.
template <class T>
T overloaded_on_return_type()
{
T var;
cout << "\n" << var;
return var;
}
The type used to call the template function has to be either an
intrinsic type or it should be some UDT that has the insertion operator
overloaded for it (atleast for this example).
Calling this function is as simple as
''overloaded_on_return_type<int>();''.
Regards,
Harish Sharma
sh**********@gmail.com wrote:
>
Calling this function is as simple as
''overloaded_on_return_type<int>();''.
Despite the name, that''s not overloading.
这篇关于我们可以在返回类型上重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!