1

I have a piece of code that originally used CString. Since it is not available to VSExpress users I replaced it with a CString "clone" found at: http://www.codeproject.com/KB/string/stdstring.aspx

This clone works just fine but one problem remains when using it:

TCHAR *GetConnectionString(){return m_szConnectionString)}; 

I get the error "no suitable conversion from "CStdStringW" to "TCHAR *" exists" and since string handling not really is my strength I don't know how to resolve this. Ok I know that I probably have to do some kind of type cast but.... The whole piece of code can be found at: Use CString in console app when using VS Express

Well, have a nice day and hopefully someone can help my with.

Regards Lumpi

Community
  • 1
  • 1
Lumpi
  • 2,697
  • 5
  • 38
  • 47

2 Answers2

1

According to the link you posted, CStdString derives from basic_string<TCHAR>. Thus, you can use its c_str() method.

const TCHAR *GetConnectionString()
{
    return m_szConnectionString.c_str();
}
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Thank you Frédéric ! Now it compiles without errors! I really hate the fact that there exist about 5billion different types of strings and chars. Ok, i understand that some different basic versions are nedded, but to me it feels like I find a new "string issue" every day.. Anyway, thank you for your help! – Lumpi Apr 23 '11 at 10:12
  • @Lumpi, you think C++ has string issues? You should try plain C sometime, so you can see what *real* string issues are (hint: *no string type* ;) – Frédéric Hamidi Apr 23 '11 at 10:17
1

Once you commit to a non-standard string class, you're stuck with having to use it. You ought to change the return value type:

CStdString GetConnectionString() {
    return m_szConnectionString;
};

The other option is to change the return type from TCHAR to const TCHAR:

const TCHAR* GetConnectionString() {
    return (LPCTSTR)m_szConnectionString;
};

Which isn't a great solution, it will fail miserably when the calling code stores the pointer and the connection string is changed. This is a flaw in the original code too btw.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you for the advice! I'll keep that issue in mind. Probably will learn it the hard way ( "it will fail miserably...") but I try to implement your advise right away. Lumpi – Lumpi Apr 23 '11 at 10:31