Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>CString has lots of internal tricks which make it look like a normal string when passed e.g. to <code>printf</code> functions, despite actually being a class - <strong>without having to cast it to <code>LPCTSTR</code> in the argument list, e.g., in the case of <code>varargs</code> (<code>...</code>) in e.g. a <code>printf</code></strong>. Thus trying to understand a single individual trick or function in the CString implementation is bad news. (The data function is an internal function which gets the 'real' buffer associated with the string.)</p> <p>There's a book, MFC Internals that goes into it, and IIRC the Blaszczak book might touch it.</p> <p>EDIT: As for what the expression actually translates to in terms of raw C++:-</p> <pre><code>TCHAR* data() { return (TCHAR*)(&amp;this[1]); }; </code></pre> <p>this says "pretend you're actually the first entry in an array of items allocated together. Now, the second item isnt actually a <code>CString</code>, it's a normal NUL terminated buffer of either Unicode or normal characters - i.e., an LPTSTR".</p> <p>Another way of expressing the same thing is:</p> <pre><code>TCHAR* data() { return (TCHAR*)(this + 1); }; </code></pre> <p>When you add 1 to a pointer to T, you actually add 1* sizeof T in terms of a raw memory address. So if one has a CString located at 0x00000010 with sizeof(CString) = 4, data will return a pointer to a NUL terminated array of chars buffer starting at 0x00000014</p> <p>But just understanding this one thing out of context isnt necessarily a good idea.</p> <p>Why do you <em>need</em> to know?</p>
 

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