C++: Can I put a child object into an array of parent objects? -
this question has answer here:
say have class called 'tile'
class tile { private: int x, y; };
and class called 'door' extends 'tile'
class door : tile { private: bool open; };
can make array of tile objects, instance create room, , have door object in same array since extension of tile? if not there alternatives use accomplish this?
can put child object array of parent objects?
no.
an object of type t
of type t
, never type, if type related. arrays contain objects of 1 single type.
if not there alternatives use accomplish this?
reference t&
, pointer t*
can refer object type derived t
. cannot store references in array though. so, may use array of pointers.
perhaps simplest , flexible choice array of shared pointers:
std::shared_ptr<tile> tiles[10];
Comments
Post a Comment