views:

206

answers:

4

I know that I can do:

class Foo;

but can I forward declare a class as inheriting form another, like:

class Bar {};

class Foo: public Bar;

?

Thanks!

+8  A: 

A forward declaration is only really useful for telling the compiler that a class with that name does exist and will be declared and defined elsewhere. You can't use it in any case where the compiler needs contextual information about the class, nor is it of any use to the compiler to tell it only a little bit about the class. (Generally, you can only use the forward declaration when referring to that class without other context, e.g. as a parameter or return value.)

Thus, you can't forward declare Bar in any scenario where you then use it to help declare Foo, and it flat-out doesn't make sense to have a forward declaration that includes the base class -- what does that tell you besides nothing?

Joe
Thanks for the downvote.
Joe
you answer is a more verbose but just as useless as the other answer with just a "No" in it. Thus a -1.
Pavel Shved
I agree, @Johnathan M Davis' answer is less verbose and tells me a lot more.
Omnifarious
"Useless"? It answers the question. I don't really see how Jonathan's answer says "a lot more". As far as I can see, they contain almost exactly the same information.
jalf
A: 

No.

Sigh. If only SO would allow single word answers.

tony
Probably because it's very rare a single word answer is useful. This is not such a case: clearly the OP needs an explanation.
GMan
yeah, I know. just having a bit of fun. The thing is, the OP didn't really give any context, so I'm not sure what there is to explain. He asked a question, the answer was no. I considered asking why he didn't just try compiling it himself!
tony
I think then you might just post a comment to the question.
UncleBens
+9  A: 

Forward declarations are declarations, not definitions. So, anything that requires the declaration of a class (like pointers to that class) need only the forward declaration. However, anything that would require the definition - i.e. would need to know the actual structure of the class - would not work with just the forward declaration.

Derived classes definitely need to know the structure of their parent, not just that the parent exists, so a forward declaration would be insufficient.

Jonathan M Davis
+1  A: 

I don't think it's useful. Consider: you have defined a class, Bar:

class Bar {
public:
    void frob();
};

Now you declare a class Foo:

class Foo;

All you can do with Foo is construct a pointer to it. Now, suppose you add the information that Foo is derived from Bar:

class Foo: public Bar;

What can you now do that you couldn't do before? I think that all you can do is accept a pointer to Foo and cast it into a pointer to Bar, then use that pointer.

void frob(Foo* f) {
    Bar *b = (Bar)f;
    b->frob();
}

However, you must have generated the pointer elsewhere, so you could have just accepted a pointer to Bar instead.

void frob(Bar* b) {
    b->frob();
}
Andrew Aylett