我对如何在C++中运行范围感到困惑。现在看来,在我的else语句中,我的finalStr被创建,然后在它离开作用域后立即被销毁。是否可以在C++中增加变量作用域?
std::string finalStr;
char curLine[128];
if(BINARY_ASCII == 1) //ignore this case please :D
{
cdata = convertBSTRToByteArray(data , numChars);
}
else
{
bstrInputString = (LPCWSTR) data;
std::strcpy(curLine, bstrInputString.operator char *());
std::string finalStr(curLine);
cout << "data is: " << finalStr.data() << "\n"; //prints the right string
}
cout << "string is: " << finalStr.data() << "\n"; //prints nothing except "string is: "
我该如何解决这个问题?我相信我需要在else语句中的复制构造函数来复制我的字符数组。有没有解决的办法?感谢您的阅读..
你的意思是除了在if语句之外声明该变量吗? –