VC++6.0里,CString类如何和字符串相互转换?CString有重载操作符吗?

 我来答
veket的小号
2014-12-02 · TA获得超过3371个赞
知道大有可为答主
回答量:3762
采纳率:0%
帮助的人:3979万
展开全部
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;
追问
说中文
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式