#include <iostream>
#include <afx.h>
using namespace std;
void main()
{
CString str1, str2, str3;
str1="0123456";
str2="789";
str1+=str2;
cout<<str1;

}
今天发现一个奇怪的问题,上面的代码当用USING MFC IN A SHARED DLL编译时有没有错误,而选USING MFC IN A STATIC LIBRARY 时有两个错误,更奇怪的是当用USING MFC IN A SHARED DLL编译时有没有错误,运行却输出了:0038413C,但是走读代码的时候看到str1的值是:0123456789,但是输出的却是:0038413C。
我用的是VC6.0,请问一下这是怎么回事呀??谢谢.....

解决方案 »

  1.   

    不懂VC。
    但是我知道要远离VC6
      

  2.   

    cout<<str1;
    --->
    cout<<(LPCTSTR)str1;
      

  3.   

    (LPCTSTR)str1; 这默认会调用转换的.所以,,LZ的问题很奇特啊.
      

  4.   

    问的莫名其妙……LZ至少要说出静态编译时的错误提示信息吧……输出为:0038413C,可能是因为cout将str1对象的地址显示出来了……(操作符“<<”没有重载)试试cout<<str1.buffer()
      

  5.   

    #include <afx.h>//afx.h要放在所有头文件的前面
    #include <iostream>
    using namespace std;
    void main()
    {
    CString str1, str2 ;
    str1="0123456";
    str2="789";
    str1+=str2;
    cout<<(LPCTSTR)str1;//类型转换
    }
    调试时发现cout那里没有强制转换的话,会调用cout的这个版本:
    _Myt& operator<<(const void *_X)
    可见输出的只是地址,但是我也不知道为什么会这样。
    看了下VC6的AFX.H和AFX.INL文件,发现CString只有一个protected成员LPTSTR m_pchData;
    看样子好像每个CString对象的前面会储存一个CStringData对象,用于保存CString的引用计数等;
    奇怪的是CStringData不是CString的成员。CStringData的定义如下:
    struct CStringData
    {
    long nRefs;             // reference count
    int nDataLength;        // length of data (including terminator)
    int nAllocLength;       // length of allocation
    // TCHAR data[nAllocLength] TCHAR* data()           // TCHAR* to managed data
    { return (TCHAR*)(this+1); }
    };