Is this a skip software design?

I am currently working on a game in C ++. since there is no garbage collector, you must always carefully remove objects, and make sure that such objects are no longer accessed after they are deleted.
Now that the project is growing, some objects can refer to more and more places. For example, my units in the game can get a link from a renderer, from a scene hierarchy, from a selector, from a HUD, and so on. Now - if an object is deleted, you need to make sure that all other classes that reference this object will be notified about it.
Or let me say that this is the opposite: if I create a new class that can refer to one of my units, I will also have to change the code of the device (or the unit manager or any other module that will delete the block if it gets destroyed) to make sure that this new module knows when the particular block that it is currently referencing is deleted.

Now I can say that to solve this problem there can be a simple event-based approach by creating a base class that one other object can subscribe to. Something like that:

class DeletableBase;//forward declaration

class ISubscriber{
public:
    virtual someObjectGotDeleted(DeletableBase* deletedObject)=0;
};

class DeletableBase{
private:
    vector<ISubscriber*> subscribers;
public:
    virtual ~DeletableBase(){
        for(int i=0; i<subscribers.size(); i++)
            subscribers[i]->someObjectGotDeleted(this);
    }
    subscribeForDeleteEvent(ISubscriber* subscriber){
        subscribers.push_back(subscriber);
    }
};

with this - if I refer to any object that inherits this class from the new class, I can simply add myself as a subscriber, and if the object is deleted from any other place, I will receive a notification about it.

Is this a "clean" coding method?

+5
4

( ), . shared_ptr, make_shared/allocate_shared boost::intrusive_ptr, .

+6

, (, ), , , , , , . , .

+1

, , . Boost smart pointers 90% .

P.S.: ++: -)

0

IMHO, . . , . map set multi , . , , .

, , , . , .

0

All Articles