CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 1999
    Location
    Pakistan
    Posts
    19

    Passing CString pointers to a function

    I have made a class with several function. I want one of my function to return several values of type string. So I have to use pointers. I am very uncomfortable with the LPCSTR kind of variable names. Can somebody give me simple example using pointers to return 2 values of CString type.
    thankx


  2. #2
    Join Date
    May 1999
    Location
    Toulouse, France
    Posts
    171

    Re: Passing CString pointers to a function

    Instead of pointers on CString, you may use a CStringArray (or CStringList) passed by reference to your function;

    void CMyClass:MyFunction(CStringList &stringList)
    {
    // empty string List
    stringlist.RemoveAll();

    // fill string List
    stringList.AddTail("string1");
    stringList.AddTail("string2");
    }

    HTH.

    K.

    Ash to ash and clay to clay, if the enemy doesn't get you, your own folk may.
    We're talking ****, 'cause life is a 'biz
    You know it is
    Everybody tryin' to get rich
    God ****!
    All I wanna do is live !

    KoRn, Children of the Korn

  3. #3
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Passing CString pointers to a function

    void CThisClass::ThisFunction(CString& p_str1, CString& p_str2)
    {
    p_str1 = "here we go";
    p_str2 = "we go we go";
    }

    will do it WITHOUT pointers, but with references

    Sally


  4. #4
    Join Date
    Apr 1999
    Posts
    383

    Re: Passing CString pointers to a function

    > Can somebody give me simple example using pointers to return 2 values of CString type

    This is the kind of thing you asked for (yuk!):

    void GetStrings(CString* pStr1, CString* pStr2)
    {
    if (pStr1)
    *pStr1 = "Replaced existing string 1";
    else
    pStr1 = new CString("Allocated new string 1");

    if (pStr2)
    *pStr2 = "Replaced existing string 2";
    else
    pStr2 = new CString("Allocated new string 2");
    }

    As you can see, how you handle the CString pointers depends on whether they already point to CStrings or not. If you allocate new CStrings, you must remember to delete them when you've finished with them. If you decide to do it this way (I think it's a big mistake), you should *never* mix heap-based and stack-based CStrings as arguments, and *always* initialize your pointers.

    I would strongly recommended you *not* to use CString pointers as arguments. If you pass references instead, the function will be simpler, safer, and clearer:

    void GetStrings(CString& str1, CString& str2)
    {
    str1 = "Replaced existing string 1";
    str2 = "Replaced existing string 2";
    }

    Dave


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

Featured