0

// this is the old MFC CString (not actually dependant on CObject) vs the new ATL version that is implemented as a template

char array[] = { 0x45, 0x46, 0x0, 0x45 };

CString a(array,sizeof array);
CString b;
int _aLen = a.GetLength();
// in vc6 _aLen is 4 
// in vs2019 _aLen is 4

b += a;

int _bLen = b.GetLength();
// in vc6 _bLen is 4 
// in vs2019 _bLen is 2! `

I can see why it is doing this. But.... Since we can give CString (one of its constructors) a pointer and a length and that actually works, why would an operator such as += or even the CString Append() function not use the length of the source string when it concatenates the string? This makes it extremely difficult to upgrade old projects to newer (visual studio) compilers. Is there a build/compile option I am missing here?

I tried changing unicode to MBCS with no difference. I also tried to step into the source but was unable to do that because I could not seem to get the source files to match the build. I was able to step into the disassembly and right up until the actual concatenation the string that was passed to operator += had length 4 but coming out the destination string only had length 2.

9
  • sizeof array is not valid C++ Commented May 16, 2024 at 18:58
  • 4
    @PepijnKramer why do you say that? Commented May 16, 2024 at 19:03
  • 2
    Under the Remarks section of the concatenation operator there is a note: "Although it is possible to create CStringT instances that contain embedded null characters, we recommend against it. Calling methods and operators on CStringT objects that contain embedded null characters can produce unintended results." Commented May 16, 2024 at 19:16
  • 5
    @PepijnKramer sizeof used with a type requires parenthesis, used with a variable or expression does not (unless operator precedence kicks in the wrong way). Personally, I always use parenthesis and don't worry about which is which. Commented May 16, 2024 at 19:42
  • 2
    @AviBerger Could well be I made that choice ages ago... never looked back ;) Commented May 16, 2024 at 20:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.