Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #4734

Re: this cast to const char*

From "Bo Persson" <bop@gmb.dk>
Newsgroups comp.lang.c++
Subject Re: this cast to const char*
Date 2011-05-05 20:21 +0200
Message-ID <92g84gFenfU1@mid.individual.net> (permalink)
References <92g50mFks4U1@mid.individual.net> <abfcbf3e-7f3a-4933-99a8-a35e36668f42@z15g2000prn.googlegroups.com> <79467adc-981a-4c96-aafb-977f276e726e@n10g2000yqf.googlegroups.com>

Show all headers | View raw


Öö Tiib wrote:
> On May 5, 8:50 pm, gwowen <gwo...@gmail.com> wrote:
>> On May 5, 6:27 pm, "Gernot Frisch" <m...@privacy.net> 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.
>>
>> Passing C++ classes to variable argument functions like printf() is
>> not advisable ... but if you *MUST* you can have your class inherit
>> from a POD type with the m_data with its only (or first) member.
>> This will force your non-POD class to be layout compatible, and so
>> the "header" (which will probably be the table of function
>> pointers for virtual dispatch). This will almost certainly work,
>> as long as the size of m_data matches the size of argument the
>> compiler expects for vararg functions...
>
> It will force only POD base subobject to be layout compatible (and
> printf does not cast argument to that base) so if it works then
> again by accident.

It actually isn't by accident (not anymore, at least), MS has 
documented that passing a CString by value gets you the pointer to 
your string. They don't dare to break all the printfs already 
existing!


Bo Persson

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

this cast to const char* "Gernot Frisch" <me@privacy.net> - 2011-05-05 19:27 +0200
  Re: this cast to const char* Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-05-05 19:39 +0200
    Re: this cast to const char* "Balog Pal" <pasa@lib.hu> - 2011-05-06 01:11 +0200
      Re: this cast to const char* Marcel Müller <news.5.maazl@spamgourmet.org> - 2011-05-06 01:54 +0200
        Re: this cast to const char* m0shbear <andrey.vul@gmail.com> - 2011-05-05 17:59 -0700
          Re: this cast to const char* James Kanze <james.kanze@gmail.com> - 2011-05-08 07:08 -0700
            Re: this cast to const char* m0shbear <andrey.vul@gmail.com> - 2011-05-08 17:03 -0700
        Re: this cast to const char* "Balog Pal" <pasa@lib.hu> - 2011-05-06 20:15 +0200
        Re: this cast to const char* James Kanze <james.kanze@gmail.com> - 2011-05-08 07:05 -0700
    Re: this cast to const char* James Kanze <james.kanze@gmail.com> - 2011-05-08 07:02 -0700
  Re: this cast to const char* Felix Bytow <felix.bytow@informatik.tu-chemnitz.de> - 2011-05-05 19:40 +0200
    Re: this cast to const char* Öö Tiib <ootiib@hot.ee> - 2011-05-05 10:52 -0700
  Re: this cast to const char* Öö Tiib <ootiib@hot.ee> - 2011-05-05 10:44 -0700
  Re: this cast to const char* gwowen <gwowen@gmail.com> - 2011-05-05 10:50 -0700
    Re: this cast to const char* Öö Tiib <ootiib@hot.ee> - 2011-05-05 10:58 -0700
      Re: this cast to const char* "Bo Persson" <bop@gmb.dk> - 2011-05-05 20:21 +0200
      Re: this cast to const char* gwowen <gwowen@gmail.com> - 2011-05-05 11:27 -0700
        Re: this cast to const char* Öö Tiib <ootiib@hot.ee> - 2011-05-05 11:37 -0700
          Re: this cast to const char* gwowen <gwowen@gmail.com> - 2011-05-05 22:28 -0700
            Re: this cast to const char* Öö Tiib <ootiib@hot.ee> - 2011-05-06 03:55 -0700
              Re: this cast to const char* "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-06 13:25 +0200
                Re: this cast to const char* Öö Tiib <ootiib@hot.ee> - 2011-05-06 06:22 -0700
  Re: this cast to const char* Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-05 22:47 -0700
  Re: this cast to const char* Goran <goran.pusic@gmail.com> - 2011-05-06 06:59 -0700
    Re: this cast to const char* Pete Becker <pete@versatilecoding.com> - 2011-05-06 10:17 -0400
      Re: this cast to const char* James Kanze <james.kanze@gmail.com> - 2011-05-08 07:13 -0700
    Re: this cast to const char* "Gernot Frisch" <me@privacy.net> - 2011-05-09 10:38 +0200
      Re: this cast to const char* Leigh Johnston <leigh@i42.co.uk> - 2011-05-09 13:03 +0100
        Re: this cast to const char* Leigh Johnston <leigh@i42.co.uk> - 2011-05-09 17:51 +0100
      Re: this cast to const char* Öö Tiib <ootiib@hot.ee> - 2011-05-09 09:40 -0700

csiph-web