Sometimes during class designing I have an issue with class organization. For example I have an Shape
class that is abstract base class for all type of shapes: rectangles, polygons, circles, etc:
Shape.h
:
#pragma
class Rectangle;
class Shape {
public:
Shape();
...
virtual Rectangle boundingRectangle() const = 0;
};
And I have an Rectangle
class derived from Shape
:
Rectangle.h
:
#pragma once
#include "Shape.h"
class Rectangle : public Shape {
public:
Rectangle();
...
Rectangle boundingRectangle() const;
};
The obvious problem using such method is that I can't forward declare Rectangle
in Shape.h
. The only solution I know is to declare Rectangle
right after Shape
in Shape.h
and in Rectangle.h
include Shape.h
(for example see QGeoShape and QGeoRectangle here).
Solution above has some disadvantages when you need to use several classes inherited from Shape
(For example virtual Circle Shape::boundingCircle() const
) in Shape
class declaration. Maybe someone know another solution?