Angus
Angus

Reputation: 12631

Typecasting in c++

void func::open()
{
 RequestSession* psg = new RequestSession;
((RequestSession*)psg)->st.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.

Upvotes: 1

Views: 166

Answers (2)

user2100815
user2100815

Reputation:

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.

Upvotes: 1

iammilind
iammilind

Reputation: 70094

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

Upvotes: 3

Related Questions