Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

closed as off-topic by Sean Middleditch, Nathan Reed, Jari Komppa, MrCranky, bummzack May 12 '14 at 9:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Sean Middleditch, Nathan Reed, Jari Komppa, MrCranky, bummzack
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Can you clarify what you want with an example of how it would be used? Also telling us how you've tried to solve it would help by giving us a starting point for helping you. –  Byte56 May 11 '14 at 23:36
    
I have tried it to solve by declaring global function Destroy, which takes CEffectPool by pointer as first parameter and index of the CEffect in CEffectPool as second parameter. CEffect calls this function, which is later defined in CEffectPool.cpp. –  Alexander May 12 '14 at 8:56

2 Answers 2

I think you don't need a pointer to a method. You already have a pointer to the CEffectPool instance.

You should do like the following code :

void CEffect::Frame() 
{
    //...

    if(_anmfrm == 24)
    {
        _pool->Destroy(_id);//you call directly the method of CEffectPool
    }
}

CEffectPool::Destroy should be public to be used by another object.

share|improve this answer
    
Isn't it a problem that CEffectPool is just predeclared class? CEffect don't know anything about the methods of CEffectPool. –  Alexander May 12 '14 at 9:40
    
You have to include the CEffectPool header in the CEffect.cpp –  Heckel May 12 '14 at 10:53

If what you are looking for is a pointer to a member function of CEffect, then it would be something in the lines:

// Assuming the type CEffect is a class with a Destroy() method
// as shown below:
class CEffect {
public:
    void Destroy() { ... };
};

// Declare a pointer-to-member type, for convenience: 
typedef void (CEffect::* DestroyFunc)();

Then you declare a member function pointer just like you would with a normal C function pointer:

DestroyFunc destroy = &CEffect::Destroy;

To call it, it is a bit different then with a C function. You will need a class instance:

CEffect * effect = new CEffect(...);

// Call CEffect::Destroy for 'effect':
(effect->*destroy)();
share|improve this answer
    
The destroying in my code is nothing else than switching it's position in CEffectPool. So I would like to have pointer to member function of CEffectPool in CEffect. –  Alexander May 12 '14 at 8:54
    
Well, just replace CEffect with CEffectPool ;) –  glampert May 12 '14 at 18:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.