これは本当にばかげた質問かもしれませんが、私が苦労してきたことです。メソッドで LPWSTR を変更した後、その特定のメソッドのみが変更され、直後に元に戻っているように見えます。グローバル変数が悪であることは知っていますが、かなりのコードを変更する必要があるため、これは私の選択ではありません。これが私がやっていることの例です:
Test.h
static LPWSTR globalStr = L"This shouldn't be here.";
// The ...s are irrelevant code.
class Test {
public:
...
void changeGlobalStr();
void testMethod();
...
...
};
テスト.cpp
#include "Test.h"
Test::changeGlobalStr() {
string testString("This should be here.");
// I manipulate testString over the next few lines so a variable is necessary.
...
BSTR bConversion = _com_util::ConvertStringToBSTR(testString.c_str());
globalStr = bConversion
// This prints out the correct output.
wcout << "globalStr in changeGlobalStr(): " << globalStr;
}
SecondTest.cpp
#include "Test.h"
Test::testMethod() {
changeGlobalStr();
// Get correct output from the print inside the method.
wcout << "globalStr in testMethod(): " << globalStr;
// Now incorrect output is printed.
}
testMethod() は、「これはここにあるはずです」ではなく、「これはここにあるべきではありません」と出力してしまいます。何が間違っているのか完全にはわかりませんが、それは初歩的なもので、C ++で非常に錆びているように感じます。