c++oopdynamic-allocation

How to keep objects created inside other objects alive without using new?


I am writing a game in C++, and I have come upon this design problem:

I have a base class called Entity for all my objects in the game. In order to use inheritance, I am storing the pointers in my octree. In my game, there are objects that will be created by other objects' methods. The problem is: where can I store the objects created this way so that it will stay for the duration of the game so that the pointers I store are valid?

I do not want to use new because I will create probably hundreds or even thousands of objects this way and I heard new is very slow.

The only way I have come up so far is to have a big vector that stores all these objects. The vector will probably be initialized with a huge size so that it won't have to resize itself and messes with my pointers. This seems rather dumb though.


Solution

  • First: don't worry too much about performance. Adhere to some basic rules for that and you'll be fine:

    most important thing for your project is: think about structure and design

    as for your concrete problem: It's fine to have a "ObjectManager" class. as a first implementation a vector is fine, too, just hide that as a implementation detail so you can change it later on if the need arises.