methods overriding and overloading

限于喜欢 提交于 2019-12-11 12:49:30

问题


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


回答1:


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




回答2:


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();
}



回答3:


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.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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



回答8:


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.



来源:https://stackoverflow.com/questions/1927587/methods-overriding-and-overloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!