简体   繁体   English

函数重载只能通过C ++中的返回值和const来完成?

[英]function overloading can only be done by return value and const in C++?

Is it possible to have two versions of the same function which are the same except for return type and constness ? 除了返回类型和常量之外,是否可以使用相同函数的两个版本?

I do not think so. 我不这么认为。 The following example shows that. 以下示例显示了这一点。 But I do not know the reason. 但我不知道原因。

#include<iostream>
using namespace std;
class A
{
 private:
       int bb ;
 public:
       double f1(int a) const {cout << "double f1 is called " << endl; return 0.0; }
       int f1(int a) {cout << "int f1 is called " << endl ; return 0; }

};

int main()
{
     int b = 6;
     A aa;

     double c= aa.f1(b);
     return 0 ;
}

output: 输出:

int f1 is called int f1被调用

Why double f1(int a) const cannot be counted ? 为什么双f1(int a)const不能计算?

You can't overload on return type, for obvious reasons. 出于显而易见的原因,您不能在返回类型上重载。

You can however overload on the constness of the implicit instance argument. 但是,您可以重载隐式实例参数的常量。 Compare: 相比:

aa.f1(b);
static_cast<A const &>(aa).f1(b);

(This example also illustrates why it makes no sense to attempt to "overload on return type": How would you make the decision in this example?) (这个例子也说明了为什么尝试“在返回类型上重载”没有意义:在这个例子中你会如何做出决定?)

I guess the main issue is that it easily introduces ambiguity. 我想主要的问题是它容易引入歧义。 For example: 例如:

int main()
{
  int b = 6;
  A aa;

  float c= aa.f1(b); // which version of f1 is called here?
  return 0 ;
}

Is it the int version and the value is promoted to a float, or is it the double version and the value is truncated? 它是int版本并且值被提升为float,还是双版本并且值被截断?

A possible solution is to move the return value to a reference parameter: 一种可能的解决方案是将返回值移动到引用参数:

void f1(int a, double &out);
void f1(int a, int &out);

and then the compiler can work out which one to call or generate an error if there's no matching type. 然后,如果没有匹配类型,编译器可以确定要调用哪一个或生成错误。

I agree with Skizz, the ambiguity is unsafe here. 我同意Skizz,这里的歧义是不安全的。 In the example you give, we never know which methods of the two will be called at last (even wondering if it could depend on the compiler?). 在你给出的例子中,我们永远不知道最后会调用哪两个方法(甚至想知道它是否依赖于编译器?)。

To my point of view, the main interest of creating member functions with the same name, but different constness in C++ is for creating const / non-const accessors on member data. 就我而言,在C ++中创建具有相同名称但不同const的成员函数的主要兴趣是在成员数据上创建const /非const访问器。

Example: 例:

class Foo {
    private: Data m_Data;

    public: Data& getData(void) { return m_Data; }
    public: const Data& getData(void) const { return m_Data; }
};

Both accessors getData() retrieve the reference of the member attribute m_Data. 两个访问器getData()都检索成员属性m_Data的引用。 The first one returns a modifiable Data when you make a call on modifiable Foo instance. 当您在可修改的Foo实例上进行调用时,第一个返回可修改的数据。 The second one returns a const Data when you make a call on a const Foo instance. 第二个在const Foo实例上调用时返回一个const数据。

Otherwise, I would advise to avoid such design. 否则,我会建议避免这种设计。

Yours, 此致,

Not unless you declare the return type as a common super type, like void* and then cast the result back to what you need. 除非您将返回类型声明为常见的超类型,例如void *,然后将结果转换回您需要的内容。 This is of course highly evil. 这当然是非常邪恶的。

You might consider wrapping the return type up in an Object which can be smart about what it contains which sort of solves the problem. 您可以考虑将返回类型包装在一个对象中,该对象可以很聪明地包含哪种解决方案。 This is what most OO systems end up doing pretty frequently, it's one reason why encapsulation can be so darn helpful. 这就是大多数OO系统最终经常做的事情,这是封装可能如此有用的一个原因。

it's also how generics are awesome. 这也是仿制药如何令人敬畏的。 I'd have to check my C++ syntax, but you can declare a function whose return type is related to its argument type, or based on the generic of the subclass. 我必须检查我的C ++语法,但是您可以声明一个函数,其返回类型与其参数类型相关,或者基于子类的泛型。

But with base-types, no, not really. 但是对于基类型,不,不是真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM