Topic: Cycles in Virtual Functions returning derived classes
Author: chris@alofi.etca.fr (Christian Millour)
Date: 1995/06/07 Raw View
In article <3r0e9t$lu5@inforamp.net>, rjbodkin@inforamp.net (Ron Bodkin) writes:
|> I understand the draft standard allows a derived class to change the return
|> type of a virtual function that returns a pointer or reference to a class
|> to return a pointer or reference to a class derived from the return class.
|> My question is whether there is any mechanism to allow for cycles of
|> derived classes to return derived classes. In other words, can one achieve
|> this effect:
|>
|> class B;
|>
|> class A
|> {
|> virtual B* makeB();
|> };
|>
|> class B
|> {
|> virtual A* myA();
|> };
|>
|> class localB;
|> // really:
|> // class localB : public B;
|>
|> class localA : public A{
|> localB* makeB();
|> };
|>
|> class localB : public B {
|> localA* myA();
|> };
|>
|> The problem is that I'd like to tell the compiler about the inheritance
|> structure of localA before defining localB and localB before localA,
|> something which I don't know how to do.
|>
Neither do I. And for an even more symmetrical example :
class BaseLeft;
class BaseRight {
virtual BaseLeft* left() const;
...
};
class BaseLeft {
virtual BaseRight* right() const;
...
};
class DerivedRight : public BaseRight {
DerivedLeft* left() const; // HOW ?????
...
};
class DerivedLeft : public BaseLeft {
DerivedRight* right() const; // OK
...
};
|> If there isn't any way to do this now, what would people think of extending
|> the standard to allow the commented out line:
|> class localB : public B; // indicate the inheritance structure of localB
|> // before defining it
|>
IMHO this should follow as a natural consequence of allowing covariant
return types.
|> This would also make it easier to include forward references instead of
|> full definitions when doing such overloading.
|>
Good point.
--chris@etca.fr
|> Thanks,
|> Ron
|>
|>
Author: rjbodkin@inforamp.net (Ron Bodkin)
Date: 1995/06/06 Raw View
I understand the draft standard allows a derived class to change the return
type of a virtual function that returns a pointer or reference to a class
to return a pointer or reference to a class derived from the return class.
My question is whether there is any mechanism to allow for cycles of
derived classes to return derived classes. In other words, can one achieve
this effect:
class B;
class A
{
virtual B* makeB();
};
class B
{
virtual A* myA();
};
class localB;
// really:
// class localB : public B;
class localA : public A{
localB* makeB();
};
class localB : public B {
localA* myA();
};
The problem is that I'd like to tell the compiler about the inheritance
structure of localA before defining localB and localB before localA,
something which I don't know how to do.
If there isn't any way to do this now, what would people think of extending
the standard to allow the commented out line:
class localB : public B; // indicate the inheritance structure of localB
// before defining it
This would also make it easier to include forward references instead of
full definitions when doing such overloading.
Thanks,
Ron