Thread: MFC-like free CString class?

  1. #1
    ad space for sale confusalot's Avatar
    Join Date
    Feb 2005
    Posts
    4

    MFC-like free CString class?

    Hi there,

    I'm looking for a free MFC-like CString class which contains the "Format"-method as well. It should be platform independent...

    I'm already searching the net for hours and I only find smart statements like "there are tons of free string classes out there" but no suitable code.

    Thanks in advance for your help!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I have a similar problem: I work with a class called _bstr_t in COM and the ubiquitous CString for MFC, and I commonly mix them up (I have to do this as MFC does like it's own CString for some functions and working with BSTRs can be nasty), but when I can get away with it I use the standard library.

    You can use a std::string and a std::stringstream with no problem, together they can easily manage your usual calls to Format. Also, I often typedef them so I can use them with UNICODE.

    Code:
    #include <windows.h>
    #include <tchar.h>
    
    #include <string>
    #include <sstream>
    
    //undefine both of these for UNICODE
    //#define UNICODE
    //#define _UNICODE
    
    namespace TSTD
    {
    	typedef std::basic_string<TCHAR> string;
    	typedef std::basic_stringstream<TCHAR> stringstream;
    }
    
    
    int main()
    {
    	TSTD::stringstream ss;
    	TSTD::string str = _T("Hello World");
    
    	ss << _T("This is an int - ") << 10000 << std::endl;
    	ss << _T("This is a floating point - ") << 5.69 << std::endl;
    	ss << _T("This is a string - \"") << str << _T("\"");;
    
    	MessageBox(HWND_DESKTOP,ss.str().c_str(),_T(""),MB_OK);	
    }

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Fordy, if you uncomment the UNICODE lines nothing will happen, because you've already included tchar.h and windows.h.
    We get the point though.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  3. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 12:44 PM
  4. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  5. MFC Inclusion of Derived CEdit class
    By Dual-Catfish in forum Windows Programming
    Replies: 3
    Last Post: 05-19-2002, 08:57 AM