void CTextObject::SetText(const VARIANT FAR& newValue)
{
CString strTemp;
ASSERT(newValue.vt == VT_BSTR);
if(m_pText != NULL) { //char* m_pText;
delete [] m_pText;
}
strTemp = newValue.bstrVal; // converts to narrow chars
m_pText = new char[strTemp.GetLength() + 1];
strcpy(m_pText, strTemp);
}特别是 strTemp = newValue.bstrVal 这句,据我了解,右边其实是unicode字符串,左边是 char,它到底调用了CString 中的哪个重载的操作符完成的,我看了CString的源码,好像没有直接 从BSTR到char*的转换的。
请高手解释一下。
{
CString strTemp;
ASSERT(newValue.vt == VT_BSTR);
if(m_pText != NULL) { //char* m_pText;
delete [] m_pText;
}
strTemp = newValue.bstrVal; // converts to narrow chars
m_pText = new char[strTemp.GetLength() + 1];
strcpy(m_pText, strTemp);
}特别是 strTemp = newValue.bstrVal 这句,据我了解,右边其实是unicode字符串,左边是 char,它到底调用了CString 中的哪个重载的操作符完成的,我看了CString的源码,好像没有直接 从BSTR到char*的转换的。
请高手解释一下。
2、newValue.bstrVal表示 _variant_t的值得字符串表示。
operator=(
wchar_t ch
);
strTemp = newValue.bstrVal;
说明该CString对象是unicode的,内部是存放的unicode编码,
strcpy(m_pText, strTemp);而这条语句是用在前面是char* ,后面是 wchar * ,并且
strcpy是只能用char*的,这才是问题关键。当中少了个unicode到ansi的转换过程。
调试跟踪,发现是执行了上面的操作符重载。里面用到了转换函数