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


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

Re: Which casting conversion to use for void*?

From "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Which casting conversion to use for void*?
Date 2011-05-10 18:24 +0200
Organization A noiseless patient Spider
Message-ID <iqboap$sun$1@dont-email.me> (permalink)
References (1 earlier) <iq81ob$ke2$1@speranza.aioe.org> <4dc8147b$0$2413$cc2e38e6@news.uslec.net> <522c2562-642a-4cd6-afbf-91013f110615@d26g2000prn.googlegroups.com> <4dc84514$0$2409$cc2e38e6@news.uslec.net> <f302dadf-21a2-4248-9d9a-ec3e9f120239@w10g2000yqa.googlegroups.com>

Show all headers | View raw


* Stuart Redmann, on 10.05.2011 08:44:
> On 9 Mai, Noah Roberts wrote:
>
> [snip]
>
>> There are certain, very rare conditions in
>> which a C-style cast does not match any of the available new-style casts
>> and is absolutely necessary for the specific cast needed.  Correctly
>> casting to a protected/private base class for example; although it's
>> extremely rare that you'd want to do this, when you do a C-style cast is
>> necessary.
>
> [snip]
>
> Could you please elaborate, preferably with an example? The following
> works just fine without any C-style cast:
>
> #include<iostream>
>
> class PrivateBase
> {
> public:
>    void f ()
>    {
>      std::cout<<  "PrivateBase";
>    }
> };
>
> class PublicDerived : private PrivateBase
> {
> public:
>    PrivateBase* GetBase ()
>    {
>      return this;
>    }
> };
>
> int main ()
> {
>    PublicDerived pd;
>    pd.GetBase ()->f ();
>    return 0;
> }
>
> Thanks in advance,
> Stuart

The problem arises when you have to use a cast, i.e. you don't control the code 
of the derived class and it doesn't offer the required access. For example, you 
might want to do this during debugging. Or for unit-testing.

Then a C style cast can correctly cast to inaccessible base as if a static_cast 
were used and had access to the base.

This includes pointer value adjustment, if needed.

I do not know what other "certain, very rare conditions" Noah refers to.

As far as the C++ standard is concerned casting to inaccessible base is the only 
thing a C style cast can actively do that named casts cannot. On the passive 
side, a C style cast can change behavior when the relevant types change (e.g. 
from const_cast to reinterpret_cast), but presumbably that's not it. However, I 
guess some compiler might implement other casting functionality as a language 
extension.


Cheers & hth.,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>

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