96
I know that I can do:
class Foo;
but can I forward declare a class as inheriting from another, like:
class Bar {};
class Foo: public Bar;
An example use case would be co-variant reference return types.
// somewhere.h
class RA {}
class RB : public RA {}
... and then in another header that doesn't include somewhere.h
// other.h
class RA;
class A {
public:
virtual RA* Foo(); // this only needs the forward deceleration
}
class RB : public RA; // invalid but...
class B {
public:
virtual RB* Foo(); //
}
The only information the compiler should need to process the declaration of RB* B:Foo() is that RB has RA as a public base class. Now clearly you would need somewhere.h if you intend to do any sort of dereferencing of the return values from Foo. However, if some clients never calls Foo, then there is no reason for them to include somewhere.h which might significantly speed compilation.