c++ - Class Objects or Pointers to their Objects? Class object combination and Implementation -
background of issue
i'm working on basic battleship-like spinoff game want continue add features on time in c++ (no graphics). have 50 x 75 game board, represented 2d vector (called gameboard) of type char. create game board , set each location '.' char. guess coordinates, guessed locations marked '-' , hits marked 'x'
where i'm @ program
i decided modify game enable more features. i'm not far along, sketching design in pseudocode started making me think more how can go upgrade.
instead of gameboard being chars, i'm creating class called block (an empty space on board), have x , y coordinate variables, along char variable visually display correct char. block has ability hold object "feature" breaks off derived classes of "feature." can scroll bottom more detail these classes.
this how class hierarchy tree goes:
feature item vehicle gold nuke plane
what need with
i have outline/structure setup want do. need kickstarting connect. i'm pretty bad determining when , how use pointers.
a.) should change gameboard hold pointers of block class? or actual block objects? - , block hold pointer feature or actual feature object?
b.) how add feature variable can empty or given value? set null?
c.) need custom copy constructor swap feature value of block?
d.) how go removing feature object block if player uses it?
e.) can there more 1 feature on single block occasionally?
f.) how declare block , feature classes such block can hold feature , feature derived class (not included in post).
extra details classes
so gameboard vector store blocks. blocks individual spaces on board. block contains coordinates location, char represent it, , possibility hold feature object, of time block won't holding feature. feature derived block , acts bonus reward in game. feature branches 2 more derived classes, item feature or vehicle. , on.
when player chooses coordinates, method go block on/in gameboard , first check if char value represents valid space hasn't been used before. checks contents of block feature. feature may empty or contain derived feature object.
ok concludes novel. sorry writing much. figure best way let helpers know what's going on. please don't respond telling me "get point." know know.. let me know if i'm missing details. thanks!
i assuming want keep class structure. @ point of abstraction suggesting using shared , uniqe pointer via (c++11 or boost). if you're pretty bad @ pointers learn how uniqe , shared pointer work , try stick those. remember keep object's life scope short possible.
a.) should change gameboard hold pointers of block class? or actual block objects? , block hold pointer feature or actual feature object?
i want keep gameboard elements immutable uniqe pointers or keep actual instances.
b.) how add feature variable can empty or given value? set null?
you have decided keep feature inside block - ok. if keep shared pointer. if there no feature shared pointer empty.
c.) need custom copy constructor swap feature value of block?
only if there dynamic/unusual inside feature. feature hold?
d.) how go removing feature object block if player uses it?
if use shared pointer there's no problem. if feature have changed during handling previous feature, feature handle correctly , destroyed when it's no longer required gameboard.
e.) can there more 1 feature on single block occasionally?
you have ask question - game require handling situation? if hold vector/map/set/array (depending on requirements) of shared pointers features.
f.) how declare block , feature classes such block can hold feature , feature derived class (not included in post).
i not sure if understand question correctly:
class feature : public gameelement { /* feature implementation */ } class block { shared_ptr<feature> mfeature; /* block implementation */ };
does answer question ?
Comments
Post a Comment