Reputation: 3
I have an object_handler class that takes care of, well, handling objects stored in its vector.
class object_handler {
private:
std::vector<object*> m_objects;
// ...
public:
void add(object* x_object);
void remove(object* x_object);
void update();
void render();
// ...
};
The object class is a basic entity I would use in my game and now what I need it to do is for it to be able to delete itself when it needs (for example, when it's health goes below zero). One way I've thought it would work is if I gave the objects a pointer to their handler so that they can access their handler's remove function, but that turned out to be a disaster: the compiler started throwing weird errors (like "expected ; before * in "object_handler* m_handler""), I guess it's because they include each-other.
What would be the right way to store game objects and handle their deleting? It would be most beneficial if the object could delete itself while heaving the vector in mind.
One other way I've though it would work is if the object would delete itself and the vector would encounter an empty pointer and erase it but I don't know how good that would be.
Upvotes: 0
Views: 161
Reputation: 41780
I usually use a flag removed
(true or false) inside the Entity
class. There I can delete them in the main loop.
Your example with the object removing itself should be okay too, as you can forward declare a class to break cyclic inclusion. But I wouldn't recommend this, as it makes when and where your objects are deleted unclear.
Upvotes: 1
Reputation: 10137
The error you describe sounds like you may want to use forward declarations rather than full includes. This will allow you to have the object_handler
class and your object
class refer to each other by references or pointers in their interface defined in the header files. The actual full includes only then need to be included in the implementation (.cpp) files and the order of inclusion won't really matter there. For example:
File object_handler.h
:
#include <vector>
// NOTE: No #include here for object class,
// only a forward reference
class object;
class object_handler {
private:
std::vector<object*> m_objects;
// ...
public:
void add(object* x_object);
void remove(object* x_object);
void update();
void render();
// ...
};
File object.h
:
// NOTE: No #include here for object_handler class,
// only a forward reference
class object_handler;
class object {
public:
object(object_handler& handler);
~object();
private:
object_handler& m_handler;
};
File object.cpp
:
// NOTE: #include both headers, order doesn't matter
#include "object.h"
#include "object_handler.h"
object::object(object_handler& handler) :
m_handler(handler)
{
m_handler.add(*this);
}
object::~object()
{
m_handler.remove(*this);
}
Whether this is a suitable way to manage your object
instances would depend on the rest of your code. The above approach is not that unusual. You may want to explore examples such as Qt's QObject
hierarchy which has analogous classes to yours (see the QObjectCleanupHandler
for example).
Upvotes: 0