Hello..
What am I doing wrong with casting?
Thanks for helpCode:class someclass {
public:
char member;
};
int main() {
someclass *sc = new someclass();
ULONG_PTR sc_pointer = static_cast<ULONG_PTR>(sc);
return 0;
}
Printable View
Hello..
What am I doing wrong with casting?
Thanks for helpCode:class someclass {
public:
char member;
};
int main() {
someclass *sc = new someclass();
ULONG_PTR sc_pointer = static_cast<ULONG_PTR>(sc);
return 0;
}
> What am I doing wrong with casting?
At the moment, everything.
C++ has 4 different kinds of cast, maybe read up on what each kind is really meant for.
Yeah, you're using the wrong cast. Try the one that starts out reint...
Thanks guys.. I used reinterpret_cast and works now..
One more question..
People often use c style cast in c++ code, is this a 'smart way' to do it? Do you use c style casts? If so, when and when not?
You should definitely read this very recent post.
Or the version that got added to the FAQ: http://cboard.cprogramming.com/showthread.php?t=86924
I have another question.
What is the right way to cast someclass (sc) so somefunc can change the pointer?
Code:class someclass {
public:
char member;
};
void somefunc(ULONG_PTR *ptr) { }
int main() {
someclass *sc;
somefunc(reinterpret_cast<ULONG_PTR*>(sc));
return 0;
}
What do you mean? If you want to change the pointer, you need to pass a pointer to it. In other words:
The other option would be to use your prototypes, but that code is 1) ugly and 2) useless. It would look like this:Code:void somefunc(ULONG_PTR ptr)
{
someclass **p = reinterpret_cast<someclass **>(ptr);
*p = new someclass;
}
int main() {
someclass *sc;
somefunc(reinterpret_cast<ULONG_PTR>(&sc));
return 0;
}
Edit: Just realized this won't actually change sc. Just sccast.Code:void somefunc(ULONG_PTR *ptr)
{
someclass *p = new someclass;
*ptr = reinterpret_cast<ULONG_PTR>(p);
}
int main() {
someclass *sc;
ULONG_PTR sccast = reinterpret_cast<ULONG_PTR>(sc);
somefunc(&sccast);
return 0;
}
However, as I said before, this is useless. The only valid reason to cast a pointer this way in the first place is if you use a WinAPI function that requires the ULONG_PTR or similar type: callbacks, SetWindowLongPtr, SendMessage. None of these takes an ULONG_PTR, so this approach is out of the question.
In all cases where you have the choice to use a ULONG_PTR*, you also have the choice to use a someclass* or someclass** or someclass*&. And all of those are far better alternatives. (Although not as good as a smart pointer.)
I use WinAPI function GetQueuedCompletionStatus thats why I need this.
GetQueuedCompletionStatus want to receive the pointer to ULONG_PTR variable to be filled with the completion key value. Why do you want instead pass the pointer to your class?
Because completion key is address of client structure? I want to change the pointer so it would point to this address.
Why do you want to cast outside the function? I think better to hide the cast inside the function
Code:someclass* getCompl()
{
ULONG_PTR temp;
if(GetQueuedCompletionStatus(...,&temp,...))
{
return reinterpret_cast<someclass*>(temp);
}
return 0;
}
The problem is I would have to return OVERLAPPED structure too, so it would be better to pass both as argument and then set it.