Thread: casting problem

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    casting problem

    Hello..

    What am I doing wrong with casting?

    Code:
    class someclass {
    public:
    	char member;
    };
    
    int main() {
    	someclass *sc = new someclass();
    	ULONG_PTR sc_pointer = static_cast<ULONG_PTR>(sc);	
    	return 0;
    }
    Thanks for help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Yeah, you're using the wrong cast. Try the one that starts out reint...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    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?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should definitely read this very recent post.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or the version that got added to the FAQ: http://cboard.cprogramming.com/showthread.php?t=86924
    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.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    630
    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;
    }

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What do you mean? If you want to change the pointer, you need to pass a pointer to it. In other words:
    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;
    }
    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 = new someclass;
      *ptr = reinterpret_cast<ULONG_PTR>(p);
    }
    
    int main() {
    	someclass *sc;
    	ULONG_PTR sccast = reinterpret_cast<ULONG_PTR>(sc);
    	somefunc(&sccast);
    	return 0;
    }
    Edit: Just realized this won't actually change sc. Just sccast.

    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

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    630
    I use WinAPI function GetQueuedCompletionStatus thats why I need this.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    630
    Because completion key is address of client structure? I want to change the pointer so it would point to this address.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. type casting problem?
    By lackofcolour in forum C Programming
    Replies: 6
    Last Post: 01-30-2006, 04:29 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM