Shandy
Shandy

Reputation: 311

How to typecast cMessage to message definition type

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 WowMessageso 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

Answers (1)

Jerzy D.
Jerzy D.

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

Related Questions