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; }
This is a discussion on casting problem within the C++ Programming forums, part of the General Programming Boards category; Hello.. What am I doing wrong with casting? Code: class someclass { public: char member; }; int main() { someclass ...
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
Yeah, you're using the wrong cast. Try the one that starts out reint...
I used to be an adventurer like you... then I took an arrow to the knee.
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.
I used to be an adventurer like you... then I took an arrow to the knee.
Or the version that got added to the FAQ: FAQ: Difference between C and C++ style casting
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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.)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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?
The first 90% of a project takes 90% of the time,
the last 10% takes the other 90% of the time.
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 first 90% of a project takes 90% of the time,
the last 10% takes the other 90% of the time.
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.