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

    to return a CString

    i defined a public class member which returns CString. (myStr = parse->getvalue("some")
    Is it good style, or its better to return an pointer to it?


  2. #2
    Join Date
    May 1999
    Location
    Texas, USA
    Posts
    568

    Re: to return a CString

    It is a lot faster to return a pointer or a reference to it. The problem is, though, if you do this it cannot be a local member. Because when it goes out of scope there is junk in memory. But if the string is part of your class, then use a pointer of a reference.

    For example:
    class CSomeClass
    {
    private:
    CString m_str;
    public:
    const CString& GetString() { return m_str; }
    };



    This would be the way to do a member string.

    But if it is not a persistant member, then the other way is fine, but there will be a little bit of performance problem because of the copying of classes.

    Maybe a better way would be to pass the CString as a parameter in the function if the string is not a member of the class.




  3. #3
    Guest

    Re: to return a CString

    There is only one problem:
    GetString() returns the ptr to a private member - you can now change the private data of the class !!!

    Return only a CString takes more time (creates a copy behind your back) but it protects the private data.

    Try it out.
    chris



  4. #4
    Join Date
    May 1999
    Location
    Texas, USA
    Posts
    568

    Re: to return a CString

    You are right it does return a reference to it. But if you define it as returning a "const" value, you are not allowed to change it.


  5. #5
    Join Date
    Aug 1999
    Location
    Dresden / Germany
    Posts
    51

    Re: to return a CString

    Oops... you are right!
    Thanks Chris


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