How can CString be converted to LPCTSTR without any operator()?
-
Hi, I have been looking for the answer to this for a long time using google and everything. I may have found some postings, but those didn't fully help me to understand this. So I checked CStringT and CSimpleStringT, and what I found is that there is only this, "operator PCXSTR()" This seems to say that CString, which is simply CSTringT, can be converted ONLY to PCXSTR. However, this code below works fine. ---------------------------------------------- CString m_str = TEXT("abcd"); LPCTSTR m_lpctstr = _str; // success ---------------------------------------------- Based on the fact that there is one operator method, which is operator PCXSTR(), I guess the complier just casts PCXSTR into LPCTSTR automatically, because this code below again works also fine. ---------------------------------------------- CString m_str = TEXT("abcd"); LPCTSTR m_lpctstr = (LPCTSTR)_str; // success ---------------------------------------------- Is this correct? Type PCXSTR is just converted to type LPCTSTR, which makes it seems that CString is also converted to LPCTSTR as if CString has LPCTSTR operator? (I am, of course, talking about this in UNICODE circumstance by the way) I think I know that I misunderstand something that could be critical, but I don't even know where to start to solve this. Thanks in advance.
-
Hi, I have been looking for the answer to this for a long time using google and everything. I may have found some postings, but those didn't fully help me to understand this. So I checked CStringT and CSimpleStringT, and what I found is that there is only this, "operator PCXSTR()" This seems to say that CString, which is simply CSTringT, can be converted ONLY to PCXSTR. However, this code below works fine. ---------------------------------------------- CString m_str = TEXT("abcd"); LPCTSTR m_lpctstr = _str; // success ---------------------------------------------- Based on the fact that there is one operator method, which is operator PCXSTR(), I guess the complier just casts PCXSTR into LPCTSTR automatically, because this code below again works also fine. ---------------------------------------------- CString m_str = TEXT("abcd"); LPCTSTR m_lpctstr = (LPCTSTR)_str; // success ---------------------------------------------- Is this correct? Type PCXSTR is just converted to type LPCTSTR, which makes it seems that CString is also converted to LPCTSTR as if CString has LPCTSTR operator? (I am, of course, talking about this in UNICODE circumstance by the way) I think I know that I misunderstand something that could be critical, but I don't even know where to start to solve this. Thanks in advance.
Dean Seo wrote:
as if CString has LPCTSTR operator
Actually, it does have one: CString::operator LPCTSTR[^]
"Real men drive manual transmission" - Rajesh.
-
Dean Seo wrote:
as if CString has LPCTSTR operator
Actually, it does have one: CString::operator LPCTSTR[^]
"Real men drive manual transmission" - Rajesh.
Rajesh R Subramanian wrote:
Actually, it does have one
In Visual Studio 6 :) This changed when CString was split off from MFC...
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Rajesh R Subramanian wrote:
Actually, it does have one
In Visual Studio 6 :) This changed when CString was split off from MFC...
Mark Salsbery Microsoft MVP - Visual C++ :java:
You're correct. However,
CSimpleStringT
does have aPCXSTR
operator defined (which when compiling for Unicode should beLPCWSTR
)."Real men drive manual transmission" - Rajesh.
-
You're correct. However,
CSimpleStringT
does have aPCXSTR
operator defined (which when compiling for Unicode should beLPCWSTR
)."Real men drive manual transmission" - Rajesh.
-
Thanks for answering my question. This helps me a lot. So consequently, in UNICODE circumstance, the complier automatically casts PCXSTR to LPCWSTR, or LPCTSTR? Also, that is why CSimpleStringT only needs operator PCXSTR()? Thank you.
Yes, Dean. You're correct. Excerpt from the header
atlsimpstr.h
template< typename BaseType = char > //_MBCS build
class ChTraitsBase
{
public:
typedef char XCHAR;
typedef LPSTR PXSTR;
typedef LPCSTR PCXSTR; //First definition here
typedef wchar_t YCHAR;
typedef LPWSTR PYSTR;
typedef LPCWSTR PCYSTR;
};template<>
class ChTraitsBase< wchar_t > //Unicode build
{
public:
typedef wchar_t XCHAR;
typedef LPWSTR PXSTR;
typedef LPCWSTR PCXSTR; //Second definition here
typedef char YCHAR;
typedef LPSTR PYSTR;
typedef LPCSTR PCYSTR;
};"Real men drive manual transmission" - Rajesh.
-
Yes, Dean. You're correct. Excerpt from the header
atlsimpstr.h
template< typename BaseType = char > //_MBCS build
class ChTraitsBase
{
public:
typedef char XCHAR;
typedef LPSTR PXSTR;
typedef LPCSTR PCXSTR; //First definition here
typedef wchar_t YCHAR;
typedef LPWSTR PYSTR;
typedef LPCWSTR PCYSTR;
};template<>
class ChTraitsBase< wchar_t > //Unicode build
{
public:
typedef wchar_t XCHAR;
typedef LPWSTR PXSTR;
typedef LPCWSTR PCXSTR; //Second definition here
typedef char YCHAR;
typedef LPSTR PYSTR;
typedef LPCSTR PCYSTR;
};"Real men drive manual transmission" - Rajesh.