views:

188

answers:

9

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

+4  A: 

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.

Andreas Brinck
+7  A: 

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

Skilldrick
This by definition of the language.
Thomas Matthews
+1  A: 

See this previously asked question

Puzzle: Overload a C++ function according to the return value

David Sykes
+1  A: 

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.

aJ
+1  A: 

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

rmn
+4  A: 

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();
}
Miroslav Bajtoš
+1  A: 

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
jk
actaully you may have to make them functors to do this properly
jk
+1  A: 

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.

Ashish
+1  A: 

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.

Rich