VC++6.0里,CString类如何和字符串相互转换?CString有重载操作符吗?
答案:1 悬赏:10 手机版
解决时间 2021-11-11 10:02
- 提问者网友:姑娘长的好罪过
- 2021-11-10 17:31
VC++6.0里,CString类如何和字符串相互转换?CString有重载操作符吗?
最佳答案
- 五星知识达人网友:狂恋
- 2021-11-10 18:35
CString::operator
LPCTSTR
operator LPCTSTR ( ) const;
Return Value
A character pointer to the string’s data.
Remarks
This useful casting operator provides an efficient method to access the
null-terminated C string contained in a CString object. No characters are
copied; only a pointer is returned. Be careful with this operator. If you change
a CString object after you have obtained the character pointer, you may
cause a reallocation of memory that invalidates the pointer.
Example
The following example demonstrates the use of CString::operator
LPCSTR.
// If the prototype of a function is known to the compiler,
// the LPCTSTR cast operator may be invoked implicitly
CString strSports(_T("Hockey is Best!"));
TCHAR sz[1024];
lstrcpy(sz, strSports);
// If the prototype isn't known, or is a va_arg prototype,
// you must invoke the cast operator explicitly. For example,
// the va_arg part of a call to sprintf() needs the cast:
sprintf(sz, "I think that %s!\n", (LPCTSTR) strSports);
// while the format parameter is known to be an LPCTSTR and
// therefore doesn't need the cast:
sprintf(sz, strSports);
// Note that some situations are ambiguous. This line will
// put the address of the strSports object to stdout:
cout << strSports;
// while this line will put the content of the string out:
cout << (LPCTSTR) strSports;追问说中文
LPCTSTR
operator LPCTSTR ( ) const;
Return Value
A character pointer to the string’s data.
Remarks
This useful casting operator provides an efficient method to access the
null-terminated C string contained in a CString object. No characters are
copied; only a pointer is returned. Be careful with this operator. If you change
a CString object after you have obtained the character pointer, you may
cause a reallocation of memory that invalidates the pointer.
Example
The following example demonstrates the use of CString::operator
LPCSTR.
// If the prototype of a function is known to the compiler,
// the LPCTSTR cast operator may be invoked implicitly
CString strSports(_T("Hockey is Best!"));
TCHAR sz[1024];
lstrcpy(sz, strSports);
// If the prototype isn't known, or is a va_arg prototype,
// you must invoke the cast operator explicitly. For example,
// the va_arg part of a call to sprintf() needs the cast:
sprintf(sz, "I think that %s!\n", (LPCTSTR) strSports);
// while the format parameter is known to be an LPCTSTR and
// therefore doesn't need the cast:
sprintf(sz, strSports);
// Note that some situations are ambiguous. This line will
// put the address of the strSports object to stdout:
cout << strSports;
// while this line will put the content of the string out:
cout << (LPCTSTR) strSports;追问说中文
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯