Results 1 to 13 of 13

Thread: Confusing char array

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Confusing char array

    I have a function inside a class called "GetCaption" that lets the user retrieve current text assigned to a button. The caption is stored in a varible in the class as:
    PHP Code:
    char *caption
    Then the "GetCaption" function is like this:
    PHP Code:
        char  CGButton::GetCaption()
        {
                if (
    CGButton::Handle == NULL)
            {
                return 
    NULL;
            }
            else
            {
                return (
    char)CGButton::caption;
            }
        } 
    Now I want to use this function like this but it's giving me an eror message "Unhandled exception in...":
    PHP Code:
        MessageBox(NULL,(char*)mybutton.GetCaption(),"hello",MB_OK); 
    Baaaaaaaaah

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Are you sure the handle exists and the appropiate code runs?

    and hmm shouldn't you dereference the pointer before returning it because the pointer will go out of scope?

    also, I think the function can be shortened to:
    PHP Code:
    char  CGButton::GetCaption()
        {
                if (
    CGButton::Handle == NULL)
                        return 
    NULL;
             return (
    char)*CGButton::caption//Notice the derefencing, try it.
        

    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That returns only the first character. Why not return a char*? (Or const char* if you don't want the string to be edited.)
    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.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You shouldn't be returning a pointer anyway, since the object might go out of scope.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Well, I think it is different when returning a member variable (that's what the char pointer is).
    But in this case (MFC extension class) it is better to store the caption in a CString and return that.
    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.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Just as an example of what I usually do and what I think you mean (feel free to pick holes):
    Code:
    class thingie {
    public:
        thingie(const string &cap) : m_caption(cap) { }
    
        thingie& operator=(const thingie &ref) {
            if(this == &ref) return *this;
    
            m_caption = ref.m_caption;
            return *this;
        }
    
        const string& caption() const { return m_caption; }
    
        // mutator method if you want it
        void caption(const string &c) { m_caption = c; }
    
    private:
        string m_caption;
    };
    For the accessor, this means that you're not penalised if you only need it during the lifetime of the object, but can persist it if you want.

    The standard strings can be easily replaced with a CString in that (it's the return-by-const-reference that's important here).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Actually I got it working after a while using something like:
    PHP Code:
    char *CGLabel::GetCaption()
    {
    return (
    char*)CGLabel::caption;

    Does this return a pointer to a local variable or it actually returns a pointer and sets its value to the value of CGLabel::caption pointer?
    Baaaaaaaaah

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It does not return a variable local to the accessor, but it may still be unsafe in some certain situations (but to be honest, I've never encountered one before).
    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.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Returning it like that is safe as long as you don't try and hold onto it after the object has been destroyed.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Ok, that's good then.
    Baaaaaaaaah

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I never felt tempted to do that...
    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.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by CornedBee
    I never felt tempted to do that...
    Do what?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I think he means:

    you don't try and hold onto it after the object has been destroyed.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

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