Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > polymorphic return types in virtual functions

Reply
Thread Tools

polymorphic return types in virtual functions

 
 
Aryeh M. Friedman
Guest
Posts: n/a
 
      02-20-2005
If I have something like this:

class NumberException {
};

class Number {
public:
...
virtual unsigned long getValue() {throw(NumberException);};
...
};

class Integer : public Number {
public:
...
virtual int getValue() {return Value;};

Integer(int i) {Value=i;};
...

private:
int Value;
};

class Float : public Number {
public:
....
virtual float getValue() {return Value};

Float(float f);
....

private:
float Value;
};

int main()
{
Float f(3.14);
Integer i(55);

cout<<f->getValue()<<" "<<i->getValue()<<endl;
}

GCC (and likelly any other ANSI compilor) barfs on getValue having different
return types while being virtual... is there any way around this problem...
the idea is I want to do a "everything is an object" style paradigm (ala
Ruby).

--Aryeh


 
Reply With Quote
 
 
 
 
Donovan Rebbechi
Guest
Posts: n/a
 
      02-21-2005
On 2005-02-20, Aryeh M. Friedman <(E-Mail Removed)> wrote:
> If I have something like this:
>
> class Number {
> public:
> virtual unsigned long getValue() {throw(NumberException);};
> };
>
> class Integer : public Number {
> public:
> virtual int getValue() {return Value;};


> class Float : public Number {
> public:
> virtual float getValue() {return Value};


Not clear on what the above is trying to achieve .. but hopefully this will
help:

Here's the problem -- consider this:

Number* x = new Integer(0);
x->getValue(); // what type does this return ?

Everything needs to return a certain type.

If you want a function to return multiple types, you need to hide the type
behind a dynamic pointer, e.g.

// always returns Number*, but the underlying object could be an Integer or
// Float
Number* f(int i, bool j) {
if (j)
return new Float(i);
else
return new Integer(i);
}

> int main()
> {
> Float f(3.14);
> Integer i(55);
>
> cout<<f->getValue()<<" "<<i->getValue()<<endl;


This is incorrect code. The correct way to write this would be something like

int main()
{
Number* f = new Float(3.14);
Number* i = new Integer(55);
std::cout << *f << " " << *i << std::endl;
}

Then you need to overload operator<<.

e.g.

class Number {
public:
virtual std::string tostring = 0;
};

std:stream& operator<< ( std:stream& s, const Number& n) {
s << n.tostring();
}

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      02-21-2005
Aryeh M. Friedman wrote:

>
> GCC (and likelly any other ANSI compilor) barfs on getValue having different
> return types while being virtual... is there any way around this problem...
> the idea is I want to do a "everything is an object" style paradigm (ala
> Ruby).
>

You can only have covariant return types from virtual functions when one
is a base class of the other. It's more specific than just being "convertable"
to the other.

The numerical types in C++ are not polymorphic. You could do it like this:

class Number {
unsigned long GetValue() { return getValueUL(); } // Not virtual.
virtual unsigned long GetValueUL() { return Value; }
};

class Float {
float GetValue() { return Value; }
unsigned long GetValueUL() { return Value(); } // definite covnersion.
};
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Polymorphic types without virtual functions Karel Miklav C++ 9 09-06-2005 06:43 PM
Polymorphic behavior without virtual functions Dave C++ 9 12-26-2003 02:08 AM
Boost + Python C/API: Mixing python return types with boost return types Steve Knight Python 2 10-10-2003 10:11 AM
Re: return types for virtual overriden functions Howard C++ 2 07-31-2003 03:03 PM
Re: return types for virtual overriden functions Makis Papapanagiotou C++ 2 07-31-2003 02:50 PM



Advertisments