Topic: dynamic_cast <const T&>
Author: Armin Pies <pies@dfki.uni-kl.de>
Date: 1998/01/15 Raw View
Does the C++ standard allow this construct?
Our compiler (SUN WSpro C++ 4.2 on Solaris 2.5) throws bad_cast.
We removed the const and all seemed to be well.
But the MS Visual C++ Compiler on Windows NT 4.0 does not like that. and
complains that it cannot cast const to non-const.
Can you help us?
Armin
--
_/_/_/ _/_/_/ _/ _/ _/ Dipl.-Inform. Armin Pies
_/ _/ _/ _/ _/ _/ Software-Ingenieur
_/ _/ _/_/_/ _/_/ _/
_/ _/ _/ _/ _/ _/ FB Informationsmanagement
_/_/_/ _/ _/ _/ _/ & Dokumentanalyse
Deutsches Forschungszentrum Tel: +49 631 205 3447
fuer Kuenstliche Intelligenz GmbH Fax: +49 631 205 3210
Postfach 2080 email: pies@dfki.de
D-67608 Kaiserslautern www: http://www.dfki.uni-kl.de/da/
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/01/15 Raw View
In article 79FC512F@dfki.uni-kl.de, Armin Pies <pies@dfki.uni-kl.de> writes:
>Does the C++ standard allow this construct?
>
>Our compiler (SUN WSpro C++ 4.2 on Solaris 2.5) throws bad_cast.
>We removed the const and all seemed to be well.
>
>But the MS Visual C++ Compiler on Windows NT 4.0 does not like that. and
>complains that it cannot cast const to non-const.
A dynamic cast may add const qualifiers, but not remove any.
A dynamic cast may be used only with pointers or references to
classes within the same hierarchy.
If you violate those rules, you should get a complaint at compile time.
If you attempt to down-cast a reference and the actual object
is not of the appropriate type, you should get a bad-cast exception
at run time.
You haven't included enough information to tell whether your code
should result in error messages or exceptions at run time.
---
Steve Clamage, stephen.clamage@sun.com
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Gerhard Huebner <ghuebner@lrt.mw.tu-muenchen.de>
Date: 1998/01/16 Raw View
> Does the C++ standard allow this construct?
>
> Our compiler (SUN WSpro C++ 4.2 on Solaris 2.5) throws bad_cast.
> We removed the const and all seemed to be well.
>
> But the MS Visual C++ Compiler on Windows NT 4.0 does not like that. and
> complains that it cannot cast const to non-const.
Let's assume the following example:
class A {
// ...
};
class B : public A {
// ...
};
B* pvb;
const B* pcb;
B bo;
const A* pca = &bo;
pcb = dynamic_cast<const B*>(pca);
pvb = const_cast<B*>(pcb);
// or:
A* pva = const_cast<A*>(pca);
pvb = dynamic_cast<B*>(pva);
Summary: to cast up from a base to a derived class pointer (or reference), you
must use dynamic_cast.
To cast away const-ness, you must use const_cast.
To do both, you must use both casts.
Regards,
Dipl.-Phys. Gerhard Huebner __
Institute of Astronautics \ \____ "Not a plucky hero
--------------------------------===>--____> until one reaches
Technische Universitaet Muenchen /_/ the Great Wall"
Tel.: +49-89-289-16015, Fax: 16004
http://www.lrt.mw.tu-muenchen.de/personen/ghuebner/ghuebner.html
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: bill@gibbons.org (Bill Gibbons)
Date: 1998/01/17 Raw View
In article <34BE21BF.79FC512F@dfki.uni-kl.de>, Armin Pies
<pies@dfki.uni-kl.de> wrote:
> Does the C++ standard allow this [ dynamic_cast <const T&> ] construct?
>
> Our compiler (SUN WSpro C++ 4.2 on Solaris 2.5) throws bad_cast.
> We removed the const and all seemed to be well.
>
> But the MS Visual C++ Compiler on Windows NT 4.0 does not like that. and
> complains that it cannot cast const to non-const.
>
> Can you help us?
The FDIS makes it clear that the result type in a dynamic_cast can
be a pointer or reference to a cv-qualified type. As with static_cast,
the result must be at least as cv-qualified as the operand.
The MS error message is correct, assuming that the operand is const.
The Sun error (bad_cast) looks like a compiler bug.
You could work around this with something like:
template<class T, class U> const T& const_dynamic_ref_cast(const U &cu) {
U& u = const_cast<U&>(cu);
T& t = dynamic_cast<T&>(u);
const T& ct = static_cast<const T&>(t);
return ct;
}
struct B { virtual void f() { } };
struct D : B { };
int main() {
const B &cb = *(new D);
const D &cd = const_dynamic_ref_cast<D>(cb);
}
(But some compilers do not support explicit template arguments on function
template calls yet.)
-- Bill Gibbons
bill@gibbons.org
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: bjorn@algonet.se (Bjorn Fahller)
Date: 1998/01/17 Raw View
On Thu, 15 Jan 1998 15:19:56, Armin Pies <pies@dfki.uni-kl.de> wrote:
> Does the C++ standard allow this construct?
What construct?
> But the MS Visual C++ Compiler on Windows NT 4.0 does not like that. and
> complains that it cannot cast const to non-const.
True. dynamic_cast helps you walk up and down the inheritance tree,
but does not let you modify (at least not remove) cv-qualifiers. Use
const_cast<T> for that.
_
/Bjorn.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]