Results 1 to 4 of 4

Thread: Casting argh

  1. #1

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471

    Casting argh

    I have the following function prototype:

    CString MyFunction(void) const

    it returns the correct string i desire.
    When i want to add the string to a listctrl i cast it the following
    way:

    lvi.pszText = (LPTSTR)(LPCTSTR)MyFunction;

    when i do this, i get garbage characters (Ý to be exact)

    However, i have another function prototype

    const CString& MyFunction(void) const

    which works correctly when i cast it. Any help?
    Bababooey
    Tatatoothy
    Mamamonkey

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Strange. You basically shouldn't cast a CString to LPTSTR anyway, but in case of the pszText member of LVITEM it should be ok. Are you sure that the function that returns the CString works correctly? You should maybe pass a non-const reference of a CString to the function and let it just set the value (pass-by-reference). If it still doesn't work the function must have a serious problem.

    Wait a moment. Is it just a typo in this post, or is your code really
    lvi.pszText = (LPTSTR)(LPCTSTR)MyFunction;
    ?
    In this case you didn't call the function but rather converted it's address to LPTSTR!!!


    BTW, C++ functions don't need the void in the parameter list...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    i just put void there so you were aware that the function doesn't alter any variable passed to it.

    as far as the altering the function address...you're right, it was a
    typo, i got it to work :P

    thx CornedBee yet again...are you gonna start charging me?
    Bababooey
    Tatatoothy
    Mamamonkey

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, not yet, this is actually fun
    And you ask good questions, not like "I want to do this app, gimme code!"
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width