polymorphic return types in virtual functions

Discussion in 'C++' started by Aryeh M. Friedman, Feb 20, 2005.

  1. 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
     
    Aryeh M. Friedman, Feb 20, 2005
    #1
    1. Advertising

  2. On 2005-02-20, Aryeh M. Friedman <> 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::eek:stream& operator<< ( std::eek:stream& s, const Number& n) {
    s << n.tostring();
    }

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, Feb 21, 2005
    #2
    1. Advertising

  3. Aryeh M. Friedman

    Ron Natalie Guest

    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.
    };
     
    Ron Natalie, Feb 21, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Makis Papapanagiotou

    Re: return types for virtual overriden functions

    Makis Papapanagiotou, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    314
    Victor Bazarov
    Jul 31, 2003
  2. Howard
    Replies:
    2
    Views:
    391
    Karl Heinz Buchegger
    Jul 31, 2003
  3. Dave
    Replies:
    9
    Views:
    391
    Victor Bazarov
    Dec 26, 2003
  4. Steve Knight
    Replies:
    2
    Views:
    782
    Steve Knight
    Oct 10, 2003
  5. Karel Miklav
    Replies:
    9
    Views:
    370
    red floyd
    Sep 6, 2005
Loading...

Share This Page