'...' in C++.

Started by
30 comments, last by Skeleton_V@T 18 years, 2 months ago
Quote:Original post by Skeleton_V@T
superpig,
Taking a deeper look at your (original) solution, a question is raised: The DoProcessMessage () takes an object of type derived from Base_Msg, so for every message types I have the same amount of respective classes, is that ?.
Well... not necessarily. In truth you can reuse message classes but assign different typecodes - e.g. if you have a set of messages which all just attach an entity ID as their data, then you could have a MessageWithEntityID class that you use for all those typecodes.

Quote:All these message classes need to be declared in a commonly-separated header, which is likely to have the same problem as the double-dispatching solution.
In the original implementation I cited, no. The implementation of a concrete message handler will need to import definitions of the message classes that it actually uses so that it can perform the typecast; it doesn't need to import message classes that it's not going to try and cast to.

In my second implementation, the base message handler needs to be aware of all the message classes, though it doesn't actually need to have them fully declared - forward declaration suffices. Without them, it can't declare all the base overrides of HandleMessage(), but because HandleMessage doesn't try to use the type at all it doesn't need to know about the actual definition of the type. Once you start writing derived message handlers that actually inspect/use the message data, then you need to import headers for the classes that you use.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Advertisement
Quote:Once you start writing derived message handlers that actually inspect/use the message data, then you need to import headers for the classes that you use.


I actually have missed that point, what a shortcoming!.

Quote:Well... not necessarily. In truth you can reuse message classes but assign different typecodes - e.g. if you have a set of messages which all just attach an entity ID as their data, then you could have a MessageWithEntityID class that you use for all those typecodes.


I thought about that, but from my point of view, it is not much different from the virtual argument solution. If I may misuse the argument, why won't I misuse the typecode to perform a bad casting ?. (although it is harder)

I have tried solidly embeding the typecode into the class itself in my previous post. It requires a reasonable amount of different classes though.
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--

This topic is closed to new replies.

Advertisement