Topic: casting with polymorphic types


Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Sat, 18 Aug 2001 12:51:49 GMT
Raw View
Hello,

In 5.2.7 "Dynamic cast" the Standard provides some examples on casting
in polymorphic class hierarchy. One situation is

 class A { virtual void f(); };
 class B : private A {};

 A* a = (A*) new B;

The cast is necessary because A is a non public base of B.

Now, the cast used is a C-style cast, and AFAIK these are deprecated.
Which one of the new casts ist appropriate here? I think only
reinterpret_cast can do it - is this really right? I.E. is

 A* a = reinterpret_cast<A*>( new B );

well defined?

Another question is why a simple static_cast cannot do this, even not
in the non-polymorphic case.

Martin



------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Sat, 18 Aug 2001 17:05:00 GMT
Raw View
MikeAlpha@NoSpam_csi.com (Martin Aupperle) writes:

| Now, the cast used is a C-style cast, and AFAIK these are deprecated.

Really?

| Which one of the new casts ist appropriate here?

The C-style cast as you wrote it.

[...]

| Another question is why a simple static_cast cannot do this, even not
| in the non-polymorphic case.

Accessibility.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]