Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 06 May 2011 09:17:51 -0500 From: Pete Becker Organization: Roundhouse Consulting, Ltd. Newsgroups: comp.lang.c++ Date: Fri, 6 May 2011 10:17:51 -0400 Message-ID: <201105061017516095-pete@versatilecodingcom> References: <92g50mFks4U1@mid.individual.net> <763ee119-dff9-40a9-9c70-69708aa13a65@18g2000prd.googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: this cast to const char* User-Agent: Unison/2.1.4 Lines: 35 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-vYl37AZumYRy0R41LZI+Xd5EVn6QHZb+AMwCYoGYa8YMwXr0w6F9vS8enfQNxE2wrePBbpynSkUOXNr!O0Flft8RUFiffXmwH1xeMSDWW0VuPARsr48zLX5d9C6FjhwPRtns2S3BeZM2v7Bp88ymxtU= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2391 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:4777 On 2011-05-06 09:59:31 -0400, Goran said: > On May 5, 7:27 pm, "Gernot Frisch" wrote: >> Hi, >> >> with "MFC" I can do: >> CString str(_T("test"); printf("%s", str); // prints "test" >> >> With my own string class, however, there seems to be a 4 byte "header" >> before the string data. >> >> I have the member >const TCHAR* m_data;< as the first member of my string >> class. >> >> How does MS doe this? > > Luck (well, cheating). First of all, as said, passing non-pod is UB. > What actually happens is that your string is passed to printf as a > POD. But CString class is made in such a way that "this" is also a > pointer to the first character (there's more to CString than a > pointer, and you don't want to know that ;-). > > So... Your code has an error, No, it doesn't. "Undefined behavior" means only that the C++ standard doesn't tell you what the code does. There's nothing preventing a compiler from defining the behavior, and there's nothing inherently wrong with relying on implementation-specific behavior. -- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book)