CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2004
    Posts
    202

    STL string VS ATL/MFC string

    when do you choose to use STL string and when do you choose to use ATL or MFC string? What's the pro's and con's of STL and ATL/MFC String?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: STL string VS ATL/MFC string

    There have been plenty of threads on this. Here is just one:

    http://www.codeguru.com/forum/showthread.php?t=319932

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Talking Re: STL string VS ATL/MFC string

    Quote Originally Posted by Paul McKenzie View Post
    There have been plenty of threads on this. Here is just one:

    [...]
    Yes, and already post #3 from the thread you linked to (from 2004!) states:

    Quote Originally Posted by wien View Post
    And please do a search.. There have been oodles of threads about this in the past.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: STL string VS ATL/MFC string

    std::string is standard. Unless you have some particular reason to do otherwise, it's always better to stick with the standard library.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: STL string VS ATL/MFC string

    It always depends...
    If working with MFC application then IMHO using CString is better (more easy and convenient for MFC classes and Windows API as well)
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: STL string VS ATL/MFC string

    It always depends...
    Very true. Even with plain Win API I prefer to deal with ATL::CString. Unless project heavily depends on STL, or there is a direct requirement on the project to use std:string strings.

    std::string is standard.
    ...which really matters for contemporary cross-platform projects. In its turn, CString strings had been considered "standard-like" in Windows world long before STL became a standard.
    Last edited by Igor Vartanov; July 14th, 2011 at 06:09 AM.
    Best regards,
    Igor

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: STL string VS ATL/MFC string

    Quote Originally Posted by Igor Vartanov View Post
    Unless project heavily depends on STL
    Not sure how you qualify heavily, but the last project I worked on that did not use STL in pretty much every compilation unit, was when I was learning C++ and nobody had bothered to teach me about STL yet.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: STL string VS ATL/MFC string

    the last project I worked on that did not use STL in pretty much every compilation unit, was when I was learning C++
    Well then you gonna be surprised about how other teams are still able to ignore STL stuff and rely on their own/third party experience/libraries. Because of lots of reasons I really respect. (You know, the word 'standard' never made me thrill )
    Last edited by Igor Vartanov; July 14th, 2011 at 01:05 PM.
    Best regards,
    Igor

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: STL string VS ATL/MFC string

    For one thing, if you aren't stuck on using VC6, ATL::CString and MFC::CString are one and the same.

    If you are coding in Windows and aren't concerned about cross platform compatibility, use CString. Why? because it has additional methods and constructors that are useful on Windows.

    For example, CString has GetBuffer.
    Code:
    CString sPath;
    GetModuleFileName( NULL, sPath.GetBuffer( MAX_PATH ), MAX_PATH );
    sPath.ReleaseBuffer( );
    It also is ANSI/UNICODE aware so you don't need to spend time with conversions.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: STL string VS ATL/MFC string

    Quote Originally Posted by Igor Vartanov View Post
    Well then you gonna be surprised about how other teams are still able to ignore STL stuff and rely on their own/third party experience/libraries. Because of lots of reasons I really respect. (You know, the word 'standard' never made me thrill )
    If they have a really good toolset which is well-known by the team, then that's cool. Everyone develops their own toolset as they go if they're smart.

    I unfortunately have met too many "programmers" who flinch at the first sign of a functor, though, and that's just not helpful.

    Quote Originally Posted by Arjay View Post
    If you are coding in Windows and aren't concerned about cross platform compatibility, use CString. Why? because it has additional methods and constructors that are useful on Windows.

    For example, CString has GetBuffer.
    Code:
    CString sPath;
    GetModuleFileName( NULL, sPath.GetBuffer( MAX_PATH ), MAX_PATH );
    sPath.ReleaseBuffer( );
    It also is ANSI/UNICODE aware so you don't need to spend time with conversions.
    Personally I find UTF-8 a lot more intuitive to deal with than UTF-16, and std::string is good enough for that. The stuff with GetBuffer can of course be done with std::vector<char>, although I'll grant it would be nice if it were available in the std::string class.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: STL string VS ATL/MFC string

    Quote Originally Posted by Lindley View Post
    Personally I find UTF-8 a lot more intuitive to deal with than UTF-16, and std::string is good enough for that. The stuff with GetBuffer can of course be done with std::vector<char>, although I'll grant it would be nice if it were available in the std::string class.
    The nice thing about CString is that it automatically handles ANSI and UNICODE when calling Windows api's, whereas std::string doesn't work so well when building in unicode. There are other methods that are helpful too like the BSTR methods when working with COM.

    Many folks mistakenly tie CString to MFC and think they need to include MFC dlls.

    This hasn't been the case in quite a while. All that is needed now is
    Code:
    #include <atlstr.h>

  12. #12
    Join Date
    Oct 2008
    Posts
    1,456

    Re: STL string VS ATL/MFC string

    Quote Originally Posted by Lindley View Post
    The stuff with GetBuffer can of course be done with std::vector<char>, although I'll grant it would be nice if it were available in the std::string class.
    note that with the new standard std::string stores chars contiguously, so this issue has been at least mitigated now.

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