Thread: return privat string form class function

  1. #1
    Unregistered
    Guest

    return privat string form class function

    please help me out...

    As the subjekt tells; I have a privat char array in a class
    and 2 public functions, one to Set Name and one to Get Name.
    but, I don't know how to return a string/char array

    I need to use the GetName function in the win32 wsprintf() fucntion, which is like the sprintf() as far as I know?!?

    so...
    how do i return a char array from a function?
    and how do I use the function return in wsprintf()???

    here is my code...

    Code:
    class Fighter
    {
    public:
        void SetName(char NameSet[]);
        char* GetName();
    private:
        char Name[];
    };
    void Fighter::SetName(char NameSet[])
    {
        strcpy(Name, NameSet);
    }
    char* Fighter::GetName()
    {
        return Name;
    }
    //somewere in winprog
    int szBuffer[100]
    wsprintf (szBuffer, TEXT ("%s"), User.GetName() );
    MessageBox (NULL, szBuffer , "Name", 0 + MB_DEFBUTTON1 + MB_ICONASTERISK);
    my problem is realy more c++ then win32 I think...

    thats all... please reply

    I can attach my full projekt if you ask!

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Firstly, change the private member > char Name[] < to either

    char *Name; OR char Name[/*a constant goes here*/];

    To return a string, simply return the name of the string.

    >> int szBuffer[100]
    That should be char szBuffer[100]

    Note that if you declare 'Name' as a char*, then you'll have to allocate memory when you use it.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    12

    this me that stratet this thread

    Thanks for your help!!!

    This is how I got it to work:
    Code:
    class Fighter
    {
    public:
        void SetName(const char * NameSet);
        char * GetName();
    private:
        char Name[100];
    };
    void Fighter::SetName(const char * NameSet)
    {
        strncpy(Name, NameSet, 100);
    }
    char* Fighter::GetName()
    {
        char * ret = new char[100];
        memcpy(ret, Name, 100);
        return ret;
    }
    //somewere in winprog
    char szBuffer[100]
    wsprintf (szBuffer, TEXT ("%s"), User.GetName() );
    MessageBox (NULL, szBuffer , "Name", 0 + MB_DEFBUTTON1 + MB_ICONASTERISK);
    -GuruN
    I'm a newbie, so don't take my words for facks!
    btw, I'm useing Dev-4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM