mfc中的隐式转换

在mfc中存在多种有对象转换为常量指针或者指针的现象,在这里我们以cstring为例


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 aCString object. No characters are copied; only a pointer is returned. Be careful with this operator. If you change aCString 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;

CString Overview Class MembersHierarchy 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值