class GameObject; //Forward declaration
class MessageBase
{
public:
MessageBase()
{}
virtual ~MessageBase()
{}
//Check to see whether we can send to this object or not. Use a
//dynamic_cast to see whether it casts to the desired vistor type or
//not, and call the relevant visitor if so!
virtual void send(GameObject *obj) const = 0;
};
class CollisionMessage;
class CollisionMessageReciever //A game object would inherit from this
{
private:
virtual void recieve(const CollisionMessage *msg) = 0;
friend class CollisionMessage;
};
class CollisionMessage : public MessageBase
{
public:
virtual void send(GameObject *obj) const
{
CollisionMessageReciever *rcvr = dynamic_cast<CollisionMessageReciever*>(obj);
if(rcvr)
rcvr->recieve(this);
}
CollisionMessageData data;
};
Issues with Acyclical Visitor
I'm attempting to use the Acyclical Visitor for passing messaged between game objects. In this case, the game objects themselves would be the visitors while the messages would be visited. The code looks something like this:
However, I get the following error: error C2681: 'GameObject *' : invalid expression type for dynamic_cast
I've done a little digging, and it may have something to do with C++ checking whether GameObject is in CollisionMessageReciever's inheritance graph at compile time. However, I haven't been able to figure out a way around this problem. I have an object defined elsewhere that inherits from both GameObject and CollisionMessageReciever, but this doesn't appear to have an effect.
It's also worth noting that at this point, GameObject has only been forward declared. However, including "GameObject.h" (with the full definition) does not resolve the compile error.
Anyone have any ideas?
Does your GameObject have virtual functions? Does your GameObject.h files directly or indirectly include the header that your message classes are defined in?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement