Skip to main content
gamedev.net
🔒 Locked

Problem: Object handle class using observer pattern (C++)

Started by ben_02 2 replies 1.3k views
Original Post
ben_02
ben_02
Hey, I need to write some kind of handle class for objects in my game such that when the object is destroyed and freed, the handle no longer points to invalid memory. My idea was to use something like the Observer pattern, and make a Publisher base class that has a list of handles that reference it. Then I could extend this class in any class in my game (such as a physical entity), and when the destructor is called, it would inform all of the handles of its destruction. This wouldn't work because destructors arn't inherited. To fix it I would have to manually make sure each of the classes destructors calls a notify() method or something in the base class. Not a very elegant solution IMO. Any ideas to solve this or better patterns you can suggest? Thanks.
SiCrane
SiCrane
What programming language are you using? In C++, a base class's destructor should be called when the destructor for the derived class is called.
Zahlman
Zahlman
C++ inherits its destructors, but you will need to mark them virtual.

I assume your problem is with telling the handles about the object destruction. Inheritance wouldn't help you there anyway, since the handles are some kind of pointer type thing anyway, and certainly aren't related by inheritance to the body objects. (Unless you're thinking of something really strange.)

The handles will still have to point *somewhere*, unless you want to somehow figure out what objects contain a handle, and set their handle pointers to null or something - and that way lies infinite recursion.

I think you want something like this structure:

class Wrapper {  // Any given Body in the system is pointed to by exactly one Wrapper,  // except for the nullBody which represents "destroyed object" and can  // be pointed to arbitrarily many times.  Body * implementation;  public:  // Put in a bunch of delegation methods here.  Wrapper() {    implementation = new Body;  }  void destroy() {    implementation = implementation.destroy();  }}class Body {  // Implement the thing here.  Body* destroy() {    delete this;    return NullBody::instance;  }}class NullBody: public Body {  // This is a Singleton.  // There are other ways around this: you could make a static instance of the  // base class be the "null object", implement no real functionality in the   // base, and use factory methods to create derived classes or return the  // null.  static NullBody * instance;  // Do other singleton stuff here  // and override main functionality methods to do whatever is appropriate for  // an "invalid object"  public:  Body* destroy() { return this; }}
ben_02
ben_02
Thanks for the help.

Actually I was just going to give the handle object a method to return the observer it points to, and then cast that to whatever object I know it is. I'm considering using templates though.

Anyway, problem is solved. I didn't know about the virtual destructors thing.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.