• Create Account

### #ActualHodgman

Posted 01 April 2013 - 04:06 AM

I need to first make a copy of the message struct

This causes all of your examples to be invalid. None of them should work.
When you take a polymorphic object, and copy it to just an instance of the base type, it's called "slicing". All of the actual derived type infomation is lost, and only the base part is copied:
http://en.wikipedia.org/wiki/Object_slicing
http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c

When you cast msg to a PickObject, this is an invalid cast. msg is a BaseMessage only; it is not a PickObject. So when you try to read the pObject member with static_cast<PickObject*>(&msg)->pObject, this member doesn't exist, and the memory access is erroneous. Often, such an illegal operation will seem to work without error, and you'll just be reading some random bit of RAM, other times you'll be lucky enough to get a crash/exception telling you that you've done something wrong.

It looks like what you want is:

void TranslatePickMessage(const BaseMessage& message)
{
const PickObject& pickMessage = static_cast<const PickObject&>(message);
m_pSelected = pickMessage.pObject;
}

### #1Hodgman

Posted 01 April 2013 - 04:03 AM

I need to first make a copy of the message struct

This causes all of your examples to be invalid. None of them should work.
When you take a polymorphic object, and copy it to just an instance of the base type, it's called "slicing". All of the actual derived type infomation is lost, and only the base part is copied:
http://en.wikipedia.org/wiki/Object_slicing
http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c

It looks like what you want is:
void TranslatePickMessage(const BaseMessage& message)
{
const PickObject& pickMessage = static_cast<const PickObject&>(message);
m_pSelected = pickMessage.pObject;
}

PARTNERS