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


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

Re: Which casting conversion to use for void*?

From Kai-Uwe Bux <jkherciueh@gmx.net>
Newsgroups comp.lang.c++
Subject Re: Which casting conversion to use for void*?
Followup-To comp.lang.c++
Date 2011-05-09 23:46 +0200
Organization University of Bielefeld
Message-ID <iq9nat$oqs$1@hoshi.visyn.net> (permalink)
References (2 earlier) <4dc8147b$0$2413$cc2e38e6@news.uslec.net> <522c2562-642a-4cd6-afbf-91013f110615@d26g2000prn.googlegroups.com> <0a6521d5-d9b2-41e7-85b4-482a6c5e51ce@b42g2000yqi.googlegroups.com> <cb935029-d46c-484f-917a-92cce1d6324e@x38g2000pri.googlegroups.com> <f62acdb2-989f-4e6a-b89b-74989bd4bff6@s2g2000yql.googlegroups.com>

Followups directed to: comp.lang.c++

Show all headers | View raw


Öö Tiib wrote:

> On May 10, 12:04 am, Joshua Maurice <joshuamaur...@gmail.com> wrote:
>> On May 9, 1:44 pm, Öö Tiib <oot...@hot.ee> wrote:
>>
>> > On May 9, 9:34 pm, Joshua Maurice <joshuamaur...@gmail.com> wrote:
>>
>> > > In all cases, the C-style cast is equivalent to either a
>> > > static_cast or a reinterpret_cast, and as you have pointed out, it
>> > > can be quite "ambiguous", or hard to tell for a human reader, which
>> > > it is when working with class types.
>>
>> > Nitpick ... in all cases C-style cast is either a static_cast,
>> > dynamic_cast, const_cast, reinterpret_cast or combination of such.
>>
>> const_cast yes, but unless I'm losing my mind, C-style casts can never
>> be dynamic_casts. Can it? Example please? I was pretty sure no, but
>> now you're making me question that. Whipping through a couple of
>> examples, I'm pretty sure no still.
> 
> Not sure ... isn't it dynamic_cast like that:
> 
>  struct OneInterface
>  {
>      virtual void one() = 0;
>  };
> 
>  struct OtherInterface
>  {
>      virtual void other() = 0;
>  };
> 
>  class X
>      : public OneInterface
>      , public OtherInterface
>  {
>  public:
>      virtual void one() {}
>      virtual void other() {}
>  };
> 
>  int main()
>  {
>      OneInterface* p = new X;
>      p->one();
>      // static_cast and reinterpret_cast both wrong:
>      OtherInterface* p2 = (OtherInterface*)p;
>      p2->other();
>  }

Assuming that the code compiles (did not check too hard), I think the line

  OtherInterface* p2 = (OtherInterface*)p;

is seen by the compiler as a reinterpret_cast as per [5.9/7] and the line

  p2->other();

is undefined behavior. In [5.4/5] and [5.4/7], you find a list of what the 
cast notation can mean; and a dynamic_cast is not listed.


Best,

Kai-Uwe Bux

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


Thread

Which casting conversion to use for void*? Nephi Immortal <immortalnephi@gmail.com> - 2011-05-08 15:44 -0700
  Re: Which casting conversion to use for void*? Ian Collins <ian-news@hotmail.com> - 2011-05-09 10:48 +1200
  Re: Which casting conversion to use for void*? Qi <no@no.no> - 2011-05-09 14:31 +0800
    Re: Which casting conversion to use for void*? Martijn van Buul <pino@dohd.org> - 2011-05-09 08:04 +0000
    Re: Which casting conversion to use for void*? Noah Roberts <dont@email.me> - 2011-05-09 09:21 -0700
      Re: Which casting conversion to use for void*? Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-09 11:34 -0700
        Re: Which casting conversion to use for void*? Noah Roberts <dont@email.me> - 2011-05-09 12:48 -0700
          Re: Which casting conversion to use for void*? Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-09 14:12 -0700
          Re: Which casting conversion to use for void*? Stuart Redmann <DerTopper@web.de> - 2011-05-09 23:44 -0700
            Re: Which casting conversion to use for void*? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-10 18:24 +0200
              Re: Which casting conversion to use for void*? Stuart Redmann <DerTopper@web.de> - 2011-05-10 22:56 -0700
        Re: Which casting conversion to use for void*? Öö Tiib <ootiib@hot.ee> - 2011-05-09 13:44 -0700
          Re: Which casting conversion to use for void*? Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-09 14:04 -0700
            Re: Which casting conversion to use for void*? Öö Tiib <ootiib@hot.ee> - 2011-05-09 14:33 -0700
              Re: Which casting conversion to use for void*? Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-05-09 23:46 +0200
                Re: Which casting conversion to use for void*? Öö Tiib <ootiib@hot.ee> - 2011-05-09 15:16 -0700
      Re: Which casting conversion to use for void*? Ian Collins <ian-news@hotmail.com> - 2011-05-10 08:02 +1200
        Re: Which casting conversion to use for void*? Öö Tiib <ootiib@hot.ee> - 2011-05-09 13:59 -0700
          Re: Which casting conversion to use for void*? Ian Collins <ian-news@hotmail.com> - 2011-05-10 09:27 +1200
            Re: Which casting conversion to use for void*? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-05-10 07:42 +0000
              Re: Which casting conversion to use for void*? Ian Collins <ian-news@hotmail.com> - 2011-05-10 20:20 +1200
                Re: Which casting conversion to use for void*? Dombo <dombo@disposable.invalid> - 2011-05-10 21:08 +0200
                Re: Which casting conversion to use for void*? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-05-11 16:23 +0000
                Re: Which casting conversion to use for void*? Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-05-11 18:57 +0200
                Re: Which casting conversion to use for void*? Öö Tiib <ootiib@hot.ee> - 2011-05-11 16:30 -0700
                Re: Which casting conversion to use for void*? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-05-12 13:54 +0000
      Re: Which casting conversion to use for void*? Qi <no@no.no> - 2011-05-10 10:45 +0800
  Re: Which casting conversion to use for void*? "Balog Pal" <pasa@lib.hu> - 2011-05-09 10:44 +0200
    Re: Which casting conversion to use for void*? ThosRTanner <ttanner2@bloomberg.net> - 2011-05-11 04:57 -0700

csiph-web