开发者

Typecasting in c++

开发者 https://www.devze.com 2023-02-28 17:53 出处:网络
void func::open() { RequestSession* psg = new RequestSession; ((RequestSession*)psg)->st开发者_如何学Go.ProVer = PRO_VERSION; //PRO_VERSION is macro
void func::open()
{
 RequestSession* psg = new RequestSession;
((RequestSession*)psg)->st开发者_如何学Go.ProVer = PRO_VERSION; //PRO_VERSION is macro
((RequestSession*)psg)->st.DevID  = DEVICE_ID; //DEVICE_ID is macro
}

I could not understand what is the need for type casting once again as psg is the pointer of RequestSession class.


There is no need of typecasting here. It's redundant and removable.


Put simply, whenever you see a C-style cast in C++ code, it is wrong. For the very, very rare occasions when a cast is needed, you should be using static_cast, or even more rarely reinterpret_cast. The only cast that appears at all in my own code is dynamic_cast, which is needed when you really need to find the type of something, but this too is quite rare.

0

精彩评论

暂无评论...
验证码 换一张
取 消