问题
I've created a class that has a bunch of inherited classes (parent classes) so that I can use polymorphism but the problem is that there are two classes that are calling each other.
So I need to forward declare them and I can forward declare one class but when I forward declare the inherited class the compiler says it can't change pointer from one to the other.
Is there a way to make the forward declaration of the inherited class so that it states it inherits from it? Ex:
class Shape;
class Circle:Shape;
回答1:
You don't need to specify the family tree, after the class name, when making forward declarations.
class Shape;
class Circle;
class Rectangle;
When you declare classes that use inheritance, the compiler would appreciate the full declaration of the parent classes.
A rule of thumb is that the types for declaration of pointers and references can be resolved using forward declarations. Any code that accesses elements of a class via pointers or references needs the complete class declaration.
来源:https://stackoverflow.com/questions/29107845/how-to-forward-declare-a-class-that-inherits-from-another-class-c