Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

New to C++ from C#, is my pointer usage correct?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 SuperV1234   Members   


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

#2 fastcall22   Moderators   


Posted 16 October 2012 - 10:31 AM

I can't comment much about the "correct" way to do an entity-component system. Though, I can comment on your C++. Overall, it's pretty well written and solid, though there's a few things missing.

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!
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#3 3DModelerMan   Members   


Posted 16 October 2012 - 06:00 PM

Yes definitely use smart pointers. I tried writing an entity system without them and it was a mess. I later rewrote it with reference counting and smart pointer "RefHandle" objects and it's worked great ever since.

#4 nox_pp   Members   


Posted 20 October 2012 - 12:12 PM

This is one of many ways to build a component based entity system. I used to do it this way, but it becomes a serious mess of entangled ownership when you start to do anything real with it. You'll ultimately determine that components can't do everything for themselves, and want to add systems that implement certain things on a group level--Renderer::Draw(...)--for instance. Then, do Entities really OWN components? Or do the corresponding systems own components? If the systems own components, what happens when a component is too simple to require a system? If Entities do own the components, then how does the rendering backend know which entities are renderable? Does it loop through all known entities and say ent->HasComponent("renderable"), or did you notify the renderer at some other time, like Renderer::RegisterRenderable(ent)? Further, you're storing your components in a vector, as opposed to an associative container like map, or unordered_map. Surely, you'll want to be able to, as I mentioned, say ent->HasComponent("renderable") or similar. Vector can do this, but it will have linear complexity.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.