I have two classes, CEffect
and CEffectPool
. A third class CLevel
, is doing most of the stuff in my code.
In every frame, it checks each CEffect
in CEffectPool
, and if it is currently inactive, it disables it.
What I would like to do is to give CEffect
the pointer to the CEffectPool
member function called Destroy()
, so I don't have to check every single CEffect
by another class whether it shall be destroyed or not.
With this function pointer, CEffect
would simply call a function, which would destroy it.
Also, I am not talking about destroying in the C++ way (I don't want memory to be freed or to call the destructor).
Here is some code, that i have, but it unfortunately don't work and I am not sure why:
CEffect.h
class CEffectPool;
class CEffect {
public:
CEffect();
CEffect(const CEffect& eff);
CEffect& operator=(const CEffect& eff);
void Initialize(CEffectPool* pool,
ushort id,
real x,
real y,
ushort timer);
void Frame();
void Draw();
bool IsActive() const;
private:
CEffectPool* _pool;
ushort _id;
CSprite _sprite;
ushort _anmfrm;
ushort _timer;
};
void DestroyEffect(CEffectPool* pool,
ushort id);
CEffect.cpp
void CEffect::Frame() {
if(_timer) {
--_timer;
return;
}
++_anmfrm;
if(_anmfrm == 24) {
DestroyEffect(_pool,_id);
}
}
CEffectPool.cpp
void CEffectPool::Destroy(ushort index) {
if(index >= _size) {
return;
}
CEffect temporary = _pool[index];
_pool[index] = _pool[_size-1];
_pool[_size-1] = temporary;
--_size;
}
void DestroyEffect(CEffectPool* pool,
ushort id) {
pool->Destroy(id);
}
Could you help me with this? What is the best approach to do this?