Let's say I want to create a little application where I can create SquaresSquare
s, CirclesCircle
s, and other shapes, display them on a screen, modify their properties after selecting them, and then compute all of their perimeters.
I now have a ShapeManagerShapeManager
, instanciatinginstantiating and storing all the shapes in an array :
Finally, I have a view with spinboxes to change each parameter for each type of shape. For example, when I select a square on the screen, the parameter widget only displays SquareSquare
-related parameters (thanks to AbstractShape::getType()AbstractShape::getType()
) and proposes to change the width of the square.
To do that I need a function allowing me to modify the width in ShapeManagerShapeManager
, and this is how I do it :
Is there a better design avoiding me to use the dynamic_castdynamic_cast
and to implement a getter/setter couple in ShapeManagerShapeManager
for each subclass variables I may have ? I already tried to use template but failed.
The problem I'm facing is not really with Shapes but with different JobsJob
s for a 3D printer (ex: PrintPatternInZoneJobPrintPatternInZoneJob
, TakePhotoOfZoneTakePhotoOfZone
, etc.) with AbstractJobAbstractJob
as their base class. The virtual method is execute()execute()
and not getPerimeter()getPerimeter()
. The only time I need to use concrete usage is to fill the specific information a job needs :
PrintPatternInZonePrintPatternInZone
needs the list of points to print, the position of the zone, some printing parameters like the temperature
TakePhotoOfZoneTakePhotoOfZone
needs what zone to take into photo, the path where the photo will be saved, the dimensions, etc...
The only time I need to use the concrete type of a Job is when I fill or display theses informations (if a TakePhotoOfZone JobTakePhotoOfZone
Job
is selected, a widget displaying and modifying the zone, path, and dimensions parameters will be shown).
The JobsJob
s are then put into a list of JobsJob
s which take the firstefirst job, executes it (by caling AbstractJob::execute()calling AbstractJob::execute()
), the goes to the next, on and on until the end of the list. (This is why I use inheritenceinheritance).
To store the different types of parameters I use a JsonObjectJsonObject
:
- problem : can't store pointers (to Pattern Or Zone
Pattern
or Zone
)
Then how would you store the concrete type of the JobJob
to use it when I have to modify the specific parameters of that type ? JobManagerJobManager
only has a list of AbstractJob*AbstractJob
*.