static cast

How can we use static cast below ?

U32 is unsigned integer

1
2
3
4
5
   ClientInfo *clientInfo = new ClientInfo(*this);
   clientInfo->msgId = *((U32*) (&msgBuff[0]));
   clientInfo->pId = *((U32*) (&msgBuff[4]));
   clientInfo->timeout = *((U32*) (&msgBuff[8]));
   clientInfo->gracePeriod = *((U32*) (&msgBuff[12]));
From what I know, to cast one type of pointer to another you need to use reinterpret_cast and not static_cast.

1
2
3
4
5
   ClientInfo *clientInfo = new ClientInfo(*this);
   clientInfo->msgId = *reinterpret_cast<U32*> (&msgBuff[0]);
   clientInfo->pId = *reinterpret_cast<U32*> (&msgBuff[4]);
   clientInfo->timeout = *reinterpret_cast<U32*> (&msgBuff[8]);
   clientInfo->gracePeriod = *reinterpret_cast<U32*> (&msgBuff[12]);

clientInfo->msgId = *(reinterpret_cast<U32*>(&msgBuff[0]));
clientInfo->pId = *(reinterpret_cast<U32*>(&msgBuff[4]));
clientInfo->timeout = *(reinterpret_cast<U32*>(&msgBuff[8]));
clientInfo->gracePeriod = *(reinterpret_cast<U32*>(&msgBuff[12]));

Solved i thing when we are doing pointer conversion from one type to other type we should use reinterpret_cast and not static_cast.


Topic archived. No new replies allowed.