Click to See Complete Forum and Search --> : CString subclass fails to work properly when used as format param


May 20th, 1999, 02:32 PM
Say for example that m_PARTY_NAME is of type CMyString. It is currently "HAMBROS"

CString tmp;
tmp.Format("Blah %s",m_PARTY_NAME);

if I try the above I get garbage in tmp.

If I do tmp.Format("Blah %s",(CString)m_PARTY_NAME) it works ok.

Looking at the dissasembly for my code:

0040C444 mov eax,dword ptr [ebp-10h]
0040C447 mov ecx,dword ptr [eax+1B8h]
0040C44D push ecx
0040C44E mov edx,dword ptr [eax+1B4h]
0040C454 push edx
0040C455 push offset string "Blah %s" (0097f124)
0040C45A lea eax,[ebp-14h]
0040C45D push eax
0040C45E call CString::Format (006c49cc)
0040C463 add esp,10h

and what happens when I pass Format a pure CString

0040C444 mov eax,dword ptr [ebp-10h]
0040C447 mov ecx,dword ptr [eax+158h]
0040C44D push ecx
0040C44E push offset string "Blah %s" (0097f124)
0040C453 lea edx,[ebp-14h]
0040C456 push edx
0040C457 call CString::Format (006c49cc)
0040C45C add esp,0Ch

You can see that the pure version pushes the string data and then the string format.
The non working one puts the string data then something else on the stack then the string format.

I think this may be due to how elipses (...) work, i.e they push the class binary contents onto the stack.
Is this true? Even if it was I'd be surprised that it didn't work as my subclass has no extra member variables, that I know of,
only member functions. Argh. Help.

dino@enterprise.net

Craig Muller
May 20th, 1999, 03:15 PM
It should not have worked in both cases. Casting your CMyString to a CString will not be enough for a Format(). You must cast to a (const char *) as this is what the Format() is expecting. The MFC String class really bites, so I wrote my own which uses an explicit operator to access the (const char *) as well as a number of extensions which MFC doesn't provide. This way you dont need ugly typecasts all over the place.

Craig.