因为有CString::operator LPCTSTR所以可以这样写
CString str;
char *p = (const char *)str;好像只有运算符才能重载吧?const char *也能重载?
2问:它又是如何实现重载左"操作符"的?

解决方案 »

  1.   

    char *p = (const char *)str;
    这不是操作符重载,而不强制类型转换。
    应该说这样写是有问题的
    char* p=str.GetBuffer();2.不行。
      

  2.   

    1.我认为是重载,如
    (LPTSTR)(LPCTSTR)str;//先重载后强制
    (LPCTSTR)str.GetBuffer();//强制转换
      

  3.   

    我同意横秋的,是重载呢,
    to 1和2楼的,你们怎么说是强制转换呢?CString是类,LPCTSTR是const char * 根本不是类型兼容,怎么可能强转!?   像int 和 long float这些兼容的类型才有可能强转嘛。你看看这样写。CString str;
    str.      打个点,后面会出现operator LPCTSTR明显告诉你是重载。我只是不明白它的实现方式。像此类似的,还有CBrush重载了HBRUSH,CDC重载了HDC,我仔细看过MSDN。孙鑫也讲HBRUSH写成这样形式是重载
    CBrush pBrush;
    HBRUSH hBr = (HBRUSH)pBrush;
      

  4.   

    MSDN的原文,我来翻译一下,大家看看对不对。
    CString::operator LPCTSTR
    operator LPCTSTR ( ) const;Return ValueA character pointer to the string’s data.ResThis 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.这个有用的类型转换操作符提供一个高效的访问方式到C风格的空终止的字符串,是CString对象的一个成员,不存在字符拷贝,仅仅返回一个指针,请小心使用该操作符,假如你在获得一个字符指针后改变一个CString对象,你将引发内存重分配,从而使得指针无效。由此看出,是操作符嘛。。
      

  5.   

    Sorrychar *p = (const char *)str;
    str=>const char * 是操作符重载,const char*到char*是转换
    operator LPCTSTR也是操作。
      

  6.   

    看了一下AFX。H,基本理解了。。谢谢各位。
      

  7.   

    好像只有运算符才能重载吧
    ------------------------
    类型转换也能重载的,很多类都重载了int或double
      

  8.   

    CString::operator LPCTSTR
    像这类本身就是C++的问题,是一个重载,
    支持实现类型的隐式转换,像C++ Primer等书讲得比较详细,去看下