I'm trying to figure out a way that I can send messages to other objects in C++. For example when an object collides with another object, it will send out a message that there was a collision that occured. I'm trying to use boost:signal's, but they don't seem to be exactly what I want, unless I'm just using them in the wrong manner.
Here's the main reason:
I don't really understand is why this code still works even known that the object I allocated on the stack has been destroyed...
class SomeClass
{
public:
void doSomething()
{
std::cout << "Testing\n";
}
};
int main(int argc, char** argv)
{
boost::signal<void ()> sig;
{
SomeClass obj;
sig.connect(std::bind(&SomeClass::doSomething, &obj));
}
sig();
}
Okay so that code outputs:
Testing
Why does it still work? Shouldn't I get some sort of run-time error or it not call the method at all? I don't really want that to actually happen, I want the connection to be lost as soon as the object is destroyed.
I was thinking of sending messages another way, perhaps Objective-C style using their type of delegates, where you would inherit from a class to have a message sent to you. But then again, it seems to lead to me for some confusion, such as if I want multiple delegates I would have an array of delegate pointers, and if a delegate gets destroyed I would have to remove it from that array... Which could cause some confusion.
e.g.
class ColliderDelegate
{
public:
virtual ~ColliderDelegate() {}
virtual void onCollisionEnter(Collider* sender, Collider* withWho) = 0;
virtual void onCollisionExit(Collider* sender, Collider* withWho) = 0;
virtual void onCollisionStay(Collider* sender, Collider* withWho) = 0;
private:
// I would have to have an array of Collider's that this object is connected to,
// so it can detatch itself when it's destroyed...
};
class Collider
{
public:
void add(ColliderDelegate* delegate);
void remove(ColliderDelegate* delegate);
void checkForCollisionWith(Collider*);
private:
// I would have to have an array of ColliderDelegate's that this object
// needs to send information to.
};
Can anyone suggest how I could implement what I want to do? I'm really confused on what I should do...