一些简单的代码在我一直使用的 sdk 中开始出现故障,而且它显然已经正常工作了很长时间,事实上我几乎可以肯定我已经编译了部分代码并且他们有工作,但最近失败了。
在调试器中确认值的示例:
void SomeFunction(CString& paramstring) //let's pretend we pass a string with the value "hello"
{
int size=paramstring.GetLength(); //size now contains an incorrect shorter value like 4
CString localstring=paramstring;//localstring contains something like "hell" or "hel"
}
为什么会发生这种情况的任何想法
最佳答案
我在控制台应用程序中尝试了您的代码,它运行良好问题来自您代码的另一部分,这里是代码尝试(使用 MFC)
#include <iostream>
#include <afx.h>
using namespace std;
//let's pretend we pass a string with the value "hello"
void fun(CString& paramstring)
{
int size=paramstring.GetLength();
cout<<"size= "<<size<<"\n";
CString localstring=paramstring;
wcout<<"string = "<<(LPCTSTR)localstring<<"\n";
}
int main()
{
CString s ("Hello");
fun(s);
}
输出:
size = 5 string = Hello
关于c++ - 在 C++ 中,如果我通过引用将 CString 传递给函数并将其分配给另一个 CString,它将被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8087566/