Re: return types for virtual overriden functions

Discussion in 'C++' started by Howard, Jul 31, 2003.

  1. Howard

    Howard Guest

    "venkat" <> wrote in message
    news:...
    > Why is this not allowed as against using reference types for return?
    >
    > class A{
    > public:
    > virtual A fun(){
    > }
    >
    > };
    >
    > class B :public A{
    > public:
    > B fun(){
    > }
    > };


    I don't know *why* it's the rule, but it is. You can't have an overridden
    virtual function differ only by return type.

    However, if you need to return different members of a class tree (i.e.,
    sometimes a base class, sometimes one of the derived classes), then you
    could consider returning a pointer to a base class object. Then the
    function fun() can create the object of the appropriate base or derived
    class, but return it as a pointer to a base class. That will allow you to
    use the pointer as needed (for example, to store it in an array or vector of
    base class pointers). For example,

    class A{
    public:
    virtual A* fun(); // create a new A object, returning pointer to an A
    };

    class B: public A
    public:
    A* fun(); // create a new B object, returning as pointer to an A
    };

    (Of course, remember to delete the dynamically-created object when done with
    it, or you'll have a memory leak.)

    -Howard
    Howard, Jul 31, 2003
    #1
    1. Advertising

  2. Howard

    Howard Guest

    "Victor Bazarov" <> wrote in message
    news:...
    > "Howard" <> wrote...
    > >
    > > "venkat" <> wrote in message
    > > news:...
    > > > Why is this not allowed as against using reference types for return?
    > > >
    > > > class A{
    > > > public:
    > > > virtual A fun(){
    > > > }
    > > >
    > > > };
    > > >
    > > > class B :public A{
    > > > public:
    > > > B fun(){
    > > > }
    > > > };

    > >
    > > I don't know *why* it's the rule, but it is. You can't have an

    overridden
    > > virtual function differ only by return type.

    >
    > First of all, overridden functions should not differ. Period.
    > The only one thing is allowed: return types can be _covariant_,
    > that is a pointer or a reference to classes within the same
    > hierarchy on a direct path:
    >
    > struct A {
    > virtual A* foo();
    > };
    >
    > struct B : A {
    > B* foo(); // overrider for A::foo()
    > };
    >
    > That's allowed. Other things have to be the same.
    >
    > Victor


    Cool! That makes sense. Thanks, Victor...I'll remember that.
    -Howard
    Howard, Jul 31, 2003
    #2
    1. Advertising

  3. Howard wrote:
    >
    > "venkat" <> wrote in message
    > news:...
    > > Why is this not allowed as against using reference types for return?
    > >
    > > class A{
    > > public:
    > > virtual A fun(){
    > > }
    > >
    > > };
    > >
    > > class B :public A{
    > > public:
    > > B fun(){
    > > }
    > > };

    >
    > I don't know *why* it's the rule, but it is. You can't have an overridden
    > virtual function differ only by return type.


    Well. Actually you can in certain circumstances. It is called 'covariant
    return types':

    class A
    {
    public:
    virtual ~A();
    virtual A* Clone();
    };

    class B: public A
    {
    public:
    virtual B* Clone();
    };

    A::~A()
    {
    }

    int main()
    {
    A* pOrig = new B;
    A* pClone = pOrig->Clone(); // calls B::Clone()
    }


    Not all compilers support covariant return types. Nevertheless
    it is legal according to the C++ standard.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jul 31, 2003
    #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:
    297
    Victor Bazarov
    Jul 31, 2003
  2. Aryeh M. Friedman
    Replies:
    2
    Views:
    1,083
    Ron Natalie
    Feb 21, 2005
  3. Steve Knight
    Replies:
    2
    Views:
    739
    Steve Knight
    Oct 10, 2003
  4. John Goche
    Replies:
    10
    Views:
    715
    Marcus Kwok
    Dec 8, 2006
  5. Replies:
    6
    Views:
    433
    Alf P. Steinbach
    Dec 20, 2006
Loading...

Share This Page