方法重写和重载

发布于 2024-08-16 04:43:36 字数 520 浏览 3 评论 0原文

我可以仅通过使用不同的返回值来重载或覆盖方法吗?在这种情况下是虚拟物质吗?

例如:

class A{

  virtual int Foo(); // is this scenario possible with/without the keyword virtual
}

class B : public A {
   virtual double Foo();
}

A* Base = new B();
int i = Base->Foo(); // will this just convert double to int ?

关于超载:

class A{

  virtual int Foo(); 
  virtual float Foo(); // is this possible ?

  int Goo();
  float Goo(); // or maybe this ?
}

Class B{
    double Foo();
}

谢谢

can i overload or override methods just by using different return value ? is virtual matter in thie case ?

for example :

class A{

  virtual int Foo(); // is this scenario possible with/without the keyword virtual
}

class B : public A {
   virtual double Foo();
}

A* Base = new B();
int i = Base->Foo(); // will this just convert double to int ?

and regarding overloading :

class A{

  virtual int Foo(); 
  virtual float Foo(); // is this possible ?

  int Goo();
  float Goo(); // or maybe this ?
}

Class B{
    double Foo();
}

thanks

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

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

发布评论

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

评论(8

迷雾森÷林ヴ 2024-08-23 04:43:36

函数的返回类型不是其签名的一部分,因此不能重载。

The return type of a function is not part of its signature, thus it can't be overloaded.

灼痛 2024-08-23 04:43:36

不,你不能那样做。

您可以在重写方法中执行的操作是返回指向某个类型的指针/引用,该类型是父级方法返回的类型的后代。

例子:

struct VA {}
struct VB : struct VA {}

class A
{
public:
    virtual VA * get();
}

class B
{
public:
    virtual VB * get();
}

No, you cannot do that.

What you can do in an overriden method is to return a pointer/reference to a type which is a descendant of the type returned by parent's method.

Example:

struct VA {}
struct VB : struct VA {}

class A
{
public:
    virtual VA * get();
}

class B
{
public:
    virtual VB * get();
}
萌梦深 2024-08-23 04:43:36

返回值不是函数签名的一部分,编译器现在如何选择哪个版本?

如果您在第一个示例中从 Foo 中删除 virtual,它将起作用并调用 Base::Foo

The return value is not part of the function signature, how would the compiler now which version to chose?

If you remove virtual from Foo in your first example it will work and call Base::Foo.

怀里藏娇 2024-08-23 04:43:36

仅更改返回值的重载是不可能的。

考虑一下:

Base->Foo();

在没有任何返回值分配的情况下调用,那么编译器无法推断出要调用哪个方法。

virtual关键字用于创建VTable,从而证明动态多态性。它对重载方法没有影响。

Overloading with only change in the return value is not possible.

consider this:

Base->Foo();

called without any return value assigning then compiler cannot deduce which method to invoke.

virtual keyword is used for creating VTable and thereby proving dynamic polymorphism. It has no impact on overloading methods.

梦途 2024-08-23 04:43:36

不幸的是,返回值不是签名的一部分,所以不是。

The return value is not part of the signature, unfortunetly, so no.

孤云独去闲 2024-08-23 04:43:36

就覆盖而言,它与类雇佣体系有关,并且有可能实现运行时多态性。

class Abstract
{
   public :  
   virtual Abstract* Clone() const = 0;
};

class Concrete1 : public Abstract  
{
   public :  
   Concrete1* Clone() const { return new Concrete1(*this); }
};

class Concrete2 : public Abstract  
{
   public :  
   Concrete2* Clone() const {  return new Concrete2(*this); }
};

根据 C++ 标准,跨范围重载是不可能的。

As far as overriding is concern it is pertaining to class hirearchy and it possible to achieve runtime polymorhism.

class Abstract
{
   public :  
   virtual Abstract* Clone() const = 0;
};

class Concrete1 : public Abstract  
{
   public :  
   Concrete1* Clone() const { return new Concrete1(*this); }
};

class Concrete2 : public Abstract  
{
   public :  
   Concrete2* Clone() const {  return new Concrete2(*this); }
};

Overloading across scopes is not possible as per C++ standard.

因为看清所以看轻 2024-08-23 04:43:36

不,您可以做一些类似的事情,例如模板化返回值(可能具有专门化)并在调用时指定模板类型,例如

template<T>
T Foo()
{
  return 0;
}

template<>
double Foo<double>()
{
  return 3.14;
}

int i = Foo<int>(); // 0
double d = Foo<double>(); //3.14

No, there are some similar thing you can do however e.g. templatize the return value (possibly with specializations) and specify the template type when calling e.g.

template<T>
T Foo()
{
  return 0;
}

template<>
double Foo<double>()
{
  return 3.14;
}

int i = Foo<int>(); // 0
double d = Foo<double>(); //3.14
青朷 2024-08-23 04:43:36

如果您使用 VC++,您将收到“重载方法仅在返回类型上不同”类型的错误。这是因为方法/函数签名不包含返回类型。我猜这是因为如果返回值没有分配给任何东西,它可能会导致歧义。例如

int iMyvar = object.method(); // 显而易见应该返回什么类型

对比:-

object.method(); // 如果返回类型是签名的一部分,编译器将调用哪个重载?模棱两可。

You'll get a "overloaded method only differs in return type" type of error if you're using VC++. This is because method/function signatures don't incorporate the return type. I'm guessing it's because of the ambiguity it can cause if the return value isn't being assigned to anything. e.g

int iMyvar = object.method(); // obvious what type should be returned

Contrast with:-

object.method(); // which overload would the compiler call if return type was part of signature? Ambiguos.

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