How to check if something in an array is valid in C++ -


i initialize array of entity pointers , add entities this...

entity* ents[16]; player* player = &player(20, 24); // subclass of entity player* player2 = &player(30, 30); ents[0] = player; ents[1] = player2; 

i loop through them 16 size since know it.

but can't them... because of them not "valid". can't run methods on entities 2-16 because didn't set are. visual studio keeps freaking out exception. try add isvalid method entity class, tried giving public boolean , set true in constructor, there's no way check data 1 of these "invalid entities" without getting exception. need able find out if entity valid first before try @ it. how?

no, can't anything elements 2-16 because invalid pointers. didn't set them null. invalid pointers. cannot call function through them or else.

you use pointer value of nullptr indicate "unfilled" array positions but, honestly, should using std::vector<std::unique_ptr<entity>> store pointers data , have no "unfilled" array positions @ all. vector grow needed.

as happens, so-called "valid" array elements dangling pointers temporaries , not "valid" in way @ all. hate visual studio allows this: rules of standard, program should not compile.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -