Topic: is dynamic_cast working on any compiler ?
Author: Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr>
Date: 1999/07/20 Raw View
I was reading about RTTI from "the C++ programming Language 3rd Edition"
And I tried to compile the examples of p412, p413
===================================================(tst.cc)
class Storable { };
class Component : public virtual Storable { };
class Receiver : public Component { };
class Transmitter : public Component { };
class Radio : public Receiver, public Transmitter { };
int main()
{
Radio r;
Radio* pr;
Receiver* prec = &r; // Receiver is ordinary base of Radio
pr = static_cast<Radio*>(prec); // ok, unchecked
pr = dynamic_cast<Radio*>(prec); // ok, run-time checked
Storable* ps = &r; // Storable is a virtual base of Radio
pr = dynamic_cast<Radio*>(ps); // ok, run-time checked
return 0;
}
============================================================
But my compiler returns an error.
It seems that dynamic_cast is not working at all.
The compiler I used was a quite new one.
You can the version of the egcs compiler and ther error messages below.
[kyagrd@haje5 kyagrd]$ g++ -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.90.27/specs
gcc version egcs-2.90.27 980315 (egcs-1.0.2 release)
[kyagrd@haje5 kyagrd]$ g++ tst.cc -o tst
tst.cc: In function `int main()':
tst.cc:14: cannot dynamic_cast `prec' (of type `class Receiver *') to type `class Radio *'
tst.cc:17: cannot dynamic_cast `ps' (of type `class Storable *') to type `class Radio *'
It is very weard that there makes no problem on static cast in line 13
but dynamic_cast in line 14 makes an error.
Is it the problem of compiler ? Doesn't egcs support dynamic_cast yet ?
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: nimel@hem1.passagen.se
Date: 1999/07/21 Raw View
In article <3794259E.94F1C4E1@daidun.kaist.ac.kr>,
Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr> wrote:
> ===================================================(tst.cc)
> class Storable { };
> class Component : public virtual Storable { };
// Change above to:
class Component : public virtual Storable
{
public:
virtual ~Component() { }
};
> class Receiver : public Component { };
> class Transmitter : public Component { };
> class Radio : public Receiver, public Transmitter { };
>
> int main()
> {
> Radio r;
> Radio* pr;
>
> Receiver* prec = &r; // Receiver is ordinary base of Radio
> pr = static_cast<Radio*>(prec); // ok, unchecked
> pr = dynamic_cast<Radio*>(prec); // ok, run-time checked
>
> Storable* ps = &r; // Storable is a virtual base of Radio
> pr = dynamic_cast<Radio*>(ps); // ok, run-time checked
>
> return 0;
> }
> ============================================================
> It is very weard that there makes no problem on static cast in line 13
> but dynamic_cast in line 14 makes an error.
> Is it the problem of compiler ? Doesn't egcs support dynamic_cast yet
?
I don't have EGCC, but my guess is that it is a problem with your code.
Dynamic_cast doesn't work with classes without run time type information
(RTTI), and classes without any virtual functions don't have RTTI.
Easiest solution to your problem is probably to add a virtual
destructor to the Component class. Pages 409-410 in "The C++
programming Language" describe this.
/Niklas
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: kuehl@horn.fmi.uni-konstanz.de (Dietmar Kuehl)
Date: 1999/07/21 Raw View
[Followups directed to one moderated newsgroups,
comp.lang.c++.moderated, only]
Hi,
Ahn Ki-yung (kyagrd@chiak.kaist.ac.kr) wrote:
: I was reading about RTTI from "the C++ programming Language 3rd Edition"
: And I tried to compile the examples of p412, p413
Maybe Bjarne has skipped the detail that you have to have at
least one virtual function in your class to have RTTI work...
: It is very weard that there makes no problem on static cast in line 13
: but dynamic_cast in line 14 makes an error.
: Is it the problem of compiler ? Doesn't egcs support dynamic_cast yet ?
egcs supports dynamic_cast, even without strange options required eg.
by a compiler we would bash all the time if we would bash compilers.
At least, it tells you what options to turn on if you use dynamic_cast.
--
<mailto:dietmar.kuehl@claas-solutions.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/07/21 Raw View
In article <3794259E.94F1C4E1@daidun.kaist.ac.kr>,
Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr> wrote:
> I was reading about RTTI from "the C++ programming Language 3rd
> Edition"
> And I tried to compile the examples of p412, p413
[example snipped]
>
> But my compiler returns an error.
> It seems that dynamic_cast is not working at all.
[snip]
The problem is that the classes have no virtual functions defined.
Without virtual functions, the classes are not polymorphic types and
dynamic_cast cannot work. If you add a virtual destructor to the base
class, it should compile OK.
--
Jim
I ignore all email from recruitment agencies.
Please do not send me email with questions - post
here.
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]