Note that there are some explanatory texts on larger screens.

plurals
  1. POGlobal scope LPWSTR reverts after changing it
    primarykey
    data
    text
    <p>This may be a really silly question but it's something I've been struggling with. After changing the LPWSTR in a method it appears to only be changing for that specific method and reverting back immediately after. I know global variables are evil but this is not my choice because it would require changing quite a bit of code. Here's an example of what I'm doing:</p> <p><b>Test.h</b></p> <pre><code>static LPWSTR globalStr = L"This shouldn't be here."; // The ...s are irrelevant code. class Test { public: ... void changeGlobalStr(); void testMethod(); ... ... }; </code></pre> <p><b>Test.cpp</b></p> <pre><code>#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 &lt;&lt; "globalStr in changeGlobalStr(): " &lt;&lt; globalStr; } </code></pre> <p><b>SecondTest.cpp</b></p> <pre><code>#include "Test.h" Test::testMethod() { changeGlobalStr(); // Get correct output from the print inside the method. wcout &lt;&lt; "globalStr in testMethod(): " &lt;&lt; globalStr; // Now incorrect output is printed. } </code></pre> <p>testMethod() ends up printing out "This shouldn't be here" instead of "This should be here". I'm not entirely sure what I'm doing wrong but I feel like it's something elementary and I'm just very rusty in my C++.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload