c++ - Which child class object exists at a certain index -


i working on assignment of making paint brush application using gp142 graphics library on c++

the hierarchy follows abstract class shape circle, polygon , openshape class inherits shape, polygon , openshape abstract rectangle, triangle, general polygon , quadrilateral inherit polygon line , curve inherit openshape in main program, have pointer

shape ** allshapes = new shape * [1]; 

as more shapes added, size increased , when removed, size reduced. want add save , load function. can save of data file how supposed know data belongs shape. example, rectangle, , line both require 2 points. similarly, in general, if want know shape on index, how know?

you aren't expected know. add base class pure virtual function, say

virtual std::ostream & serialize(std::ostream & out) = 0; 

which must implemented each subclass , performs serialization specific subclass. likewise, can add

std::string & whatami() = 0; 

function tell shape is, using information in code problematic. example, do if coder adds class mobiusparallelogram returns unrecognized string? better embrace encapsulation , have of subclass specific stuff hidden , handled inside object.

reiterating point on using std:vector on performing own array management. save lot of time , trouble.

if cannot use std:: vector in constraints of assignment , have hard way, don't allocate 1 @ time because program spending lot of time resizing , copying data old array new array, plus behind scenes activities resulting fragmenting memory large number of allocations , deallocations. consider doubling size every time array needs resized , don't shrink array unless have to. keeping array exact right size gains nothing, pointer array doesn't know how big contained array is.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -