Members - Reputation: 405
Posted 16 October 2012 - 09:33 AM
I've started learning C++ and wanted to make sure I'm not doing anything wrong. I've designed a very simple component-based entity system intended for game development.
An EntityManager manages pointers to Entity objects, and an Entity manages pointers to Component objects.
Game behavior is written by having classes that inherit from Component and override its methods.
The compiler was complaining about not having a virtual destructor in Component and I added one.
The Factory class is supposed to be an easy way to create Entity objects with preset component combinations.
This system is obviously not ready for game creation and missing many features, but the main point here is my memory management and pointer usage.
Is this the correct way to do it? What would you change?
Sorry if this is an unusual/uninteresting post, but I want to make sure I learn good practices from the beginning. Thanks for the help
Crossbones+ - Reputation: 9182
Posted 16 October 2012 - 10:31 AM
1) Entity will need a destructor to cleanup its components.
2) Entity and EntityManager will either need the Rule of Three satisfied or made copyable in order to prevent object-ownership issues on (accidental or otherwise) copying Entities or EntityManagers.
Other things to consider:
1) Consider using smart pointers over raw pointers to give additional context to each pointer, add additional levels of safety, and provide automatic cleanup. (For comparison, an object reference in C# is equivalent to a shared_ptr in C++.) Since it appears your compiler has support for some C++11 or C++TR1 features (use of the auto keyword and the C++11 for-in syntax), include memory and you should be good to go!
gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=
Members - Reputation: 1173
Posted 16 October 2012 - 06:00 PM
Members - Reputation: 499
Posted 20 October 2012 - 12:12 PM
Other than that, as fastcall mentioned, you're leaking memory by not deleting your Component* pointers when your Entities are destroyed. Every call to new needs to be balanced with a corresponding call to delete.
And with regard to smart pointers, I wouldn't use them until you determine that you really need them to accomplish some goal. They're quite often just a way of skirting around the issue of knowing what memory is used and where. Then again, other than that, there isn't a tremendous downside to them, so most will disagree with me here. They're a decent solution to the entangled ownership problem that I mentioned, say storing shared_ptr inside Entity and weak_ptrs inside of systems.
Between Scylla and Charybdis: First Look <-- The game I'm working on
Object-Oriented Programming Sucks <-- The kind of thing I say