Covariant return types and interdependent classes
I'm having a problem trying to get two classes that refer to each
other take a covariant return type. While this is occurring in the
Visual C++ 2010 compiler (and I'm posting a modified version here of
my original post in the C++ forums at MS), I have a more general
question, namely if there is a way for a compiler to pass what would
be, in my opinion, valid code for covariant return types, or if I'm at
the mercy of the compiler vendor :-)
In base1.h:
struct base2; // fwd decl
struct base1 {
virtual base2* getBase2() const = 0;
};
- - -
In base2.h:
struct base1; // fwd decl
struct base2 {
virtual base1* getBase1() const = 0;
};
- - -
In derived1.h:
#include "base1.h"
// necessary for compiler to see inheritance relationship
// between base2 and derived2 and allow covariant return type.
#include "derived2.h"
struct derived1: public base1 {
derived2* getBase2() const { ... }
};
- - -
In derived2.h:
#include "base2.h"
// necessary for compiler to see inheritance relationship
// between base1 and derived1 and allow covariant return type.
#include "derived1.h"
struct derived2: public base2 {
derived1* getBase1() const { ... }
};
In my opinion, this should be valid C++ code and in the case of the
compiler mentioned above, it fails to compile because of a
"technicality": to see the inheritance relationship one must include
derived2.h in derived1.h so it can see derived2 is-a base2, and
include the derived1.h in derived2.h so it can see derived1 is-a
base1, since a simple forward declaration for derived 2 and derived1
does not work. AS LONG AS I keep it simple and don't have
interdependent classes, covariant return types work as advertised.
If this is valid C++ code, my question is if there is a compiler that
works with this code (perhaps EDG's), and if not, is it a problem in C+
+ itself in specifying something that may not be practically met
(albeit a counter argument would be that a multi-pass compiler should
be smart enough to detect the inheritance relationship :-)
Cheers,
Javier
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]