How to forward declare a class that inherits from another class c++?

半腔热情 提交于 2020-01-30 06:48:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!