In C++, is it possible to forward declare a class as inheriting from another class?

后端 未结 5 1455
忘了有多久
忘了有多久 2020-11-29 04:46

I know that I can do:

class Foo;

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

class Bar {};

class F         


        
5条回答
  •  心在旅途
    2020-11-29 05:24

    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?

提交回复
热议问题