Reputation: 311
I have created a message type WowMessage
by using OMNeT++'s message definition tool. Suppose that the class Server
's overriden handleMessage
function always receives a WowMessage
message, which is initially handled as a cMessage
. How would I go about typecasting from cMessage
to WowMessage
so I can make used of the defined member fields and functions of the WowMessage
type?
void Server::handleMessage(cMessage *msg)
{
// Receives WowMessage which is subclassed from cMessage...
// TODO: typecast from cMessage to WowMessage
forwardMessage(msg);
}
void Server::forwardMessage(WowMessage *msg)
{
send(msg, "port$o", msg->getDestAddress() - 1);
}
Upvotes: 0
Views: 88
Reputation: 7002
Use dynamic_cast
, for example this way:
WowMessage * wowMsg = dynamic_cast<WowMessage *>(msg);
if (wowMsg) {
// msg is an instance of WowMessage
} else {
// another message
}
Upvotes: 1