POV-Ray : Newsgroups : povray.off-topic : Standard libraries : Re: Standard libraries Server Time
6 Sep 2024 11:20:32 EDT (-0400)
  Re: Standard libraries  
From: Nicolas Alvarez
Date: 8 Mar 2009 19:33:48
Message: <49b455dc@news.povray.org>
Darren New wrote:
> Warp wrote:
>> that objects could register themselves to some centralized, non-template
>> factory, using their own typeid as key, so that the factory could create
>> clones of those objects at runtime, based on those typeids.)
> 
> Actually, wouldn't this be clearer to do with virtual functions or
> pointers to functions anyway? Where does typeid make things better in that
> scenario? Isn't it just as easy to do something like
> 
> class Alpha : Beta {
>     ....
>     virtual const char * me() { return "Alpha:Beta"; }
>     ....
> }
> 
> and switch on that?  That even gives you a defined value, so you can (say)
> write it into a file and read it back on the next run to reload objects
> that serialized themselves or something.

I think the most useful feature of RTTI is not typeid() to get a string, but
dynamic_cast.

void foo(Alpha* ptr) {
    Beta* beta = dynamic_cast<Beta*>(ptr);
}

If ptr is a Beta object that was (maybe implicitly) cast to a Alpha*, then
beta is now equal to ptr (except its type is Beta*, so you can use Beta
members). If ptr actually pointed to an Alpha, or a Delta (another subclass
of Alpha), then the cast will throw a std::bad_cast exception.

In other words, dynamic_cast behaves like Java typecasts. We just call it
std::bad_cast instead of java.lang.ClassCastException.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.