Quantcast
Channel: C++: Array of parent and child objects - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by Ernest Friedman-Hill for C++: Array of parent and child objects

$
0
0

In general, you can't make a C++ array whose elements are of different class types; each array element must be the same size, and that won't necessarily be true of different subclasses. Putting different types in the the array can result in slicing, which is the surprising disappearance of the data members of a subclass.

But if you make an array of pointers to your different subclasses, then you'll easily be able to do what you want. You might also consider using a vector instead of an array if the number of players can vary.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>