Original Post
Is it safe to delete a void pointer without casting it to its base type? Will it always delete the right amount of memory? Assume that I never allocate arrays, only single objects. For example:
enum DataType
{
DATA_INT,
DATA_STRING
};
class GameMessage
{
public:
GameMessage( DataType Type, void* pData )
{
switch( Type )
{
case DATA_INT:
m_pData = new int;
*(int*)m_pData = *(int*)pData;
break;
case DATA_STRING:
m_pData = new std::string;
*(std::string*)m_pData = *(std::string*)pData;
break;
}
m_DataType = Type;
}
~GameMessage()
{
delete m_pData; // Is this correct?
// Or maybe I should do:
switch( m_DataType )
{
case DATA_INT:
delete (int*)m_pData;
break;
case DATA_STRING:
delete (std::string*)m_pData;
break;
}
}
void* m_pData;
DataType m_DataType;
};