Reputation: 31
I have a routine that gets passed a void* to a structure. In the routine I cast it to the correct type, e.g.
SomeFunc(void* pMsg) {
printf("Event = %d\n", static_cast<struct MyEventMsg*>(pMsg)->Event);
}
If I am accessing many members of the structure the code looks pretty messy. I can create a new pointer of the correct type and assign it the cast void*, but it seems like it shouldn't be necessary to create a temporary variable just for this purpose. Is there some way to cast the parameter pMsg to the correct type?
Thanks Doug
Upvotes: 3
Views: 590
Reputation: 99094
The way to do what you ask is
SomeFunc(void* pMsg) {
MyEventMsg * pM = static_cast<struct MyEventMsg*>(pMsg);
printf("Event = %d\n", pM->Event);
printf("Something Else = %d\n", pM->SomeOtherMember);
...
}
But I would recommend something more C++:
SomeFunc(MyEventMsg &Msg) { // consider making this const
cout << "Event = " << Msg.Event << endl;
cout << "Something Else = " << Msg.SomeOtherMember << endl;
...
}
Upvotes: 2
Reputation: 7003
Change the type of pMsg
to struct MyEventMsg *
and create a second, inline version of the method that takes a void *
, casts it and calls the first one.
You can prevent calls to the first method by making it private.
Upvotes: 0
Reputation: 208353
The first question is why does the function take a void*
in the first place. If that is changed to the exact type of pointer, then the problem goes away altogether.
If you are going to use the pointer (after casting to the appropriate type) more than once in the function, I would just create a local variable of the given type, cast once and then use it. As of the cost of creating such a variable, in many cases the compiler will optimize the variable away (if you doubt, look at the generated assembly).
Upvotes: 1
Reputation: 13150
Creating the pointer of the correct type and assigning it the cast of the void* is the correct way to go. It will make the code shorter and easier to read, if you choose a "good" name for the pointer.
You say that you think the pointer "unnecessary". If this is because you think that there is a run time cost associated with it, you may well find out that the compiler will eliminate the pointer for you.
Upvotes: 4
Reputation: 5963
Just go with the temporary. If you manage everything properly (if any managing is even needed), it won't cause you any problems. I'd imagine making the temp will be faster as well since you'd only be making one cast as opposed to casting EVERYTIME for EVERY member of the struct.
Upvotes: 1
Reputation: 503865
but it seems like it shouldn't be necessary to create a temporary variable just for this purpose
Um, it's not: like you said, you can do a cast in-place.
Make your choice: cast every time (messy) or cast once into a variable of the correct type (clean).
Upvotes: 0
Reputation: 133004
No, as a matter of fact it should be. Static cast is your best option. Don't worry about the temporary. It will most likely be optimized away by your compiler
Upvotes: 1