Deriving from VTK classes?

M.Schrijver at el.utwente.nl M.Schrijver at el.utwente.nl
Fri Sep 17 08:08:09 EDT 1999


Hello Roland,

> ----------
> From: 	Roland Schwarz[SMTP:roland.schwarz at telecom.at]
> Sent: 	Friday, September 17, 1999 1:06 PM
> To: 	users VTK
> Subject: 	Deriving from VTK classes?
> 
> Dear VTK users,
> 
>     every class in the VTK library implements the
> 'GetClassName' function which allows to check for
> the correct class before calling a member function
> (late binding). 
Correct, but it is discouraged to use it like that in languages like C++
! Only use it for debugging purposes. Or, if you really have to, in
script languages like Tcl. C++ does it's own type checking at
compile-time (even for late-binding!!!).

> If I have a pointer to a derived class, the class name
> is that of the derived class, but the meber function
> is the original one, and could still be called. Since
> the class name is different from the parent class,
> there seems to be no way to figure out what the parent 
> class actually is. I would expect some pointer to the 
> class name of the parent, so I could walk the class 
> hierarchy.
The complete hierarchy is fixed at compile-time! So, you don't have to
figure out the parent; you simply KNOW it. By the way, which member
function is called depends on whether or not the derived class(es)
redefines the functions. So, you don't "know" which implementation is
called ..... That's exactly the beauty of it! 

> In the book of Bjarne Stroustrup I found that there is a
> concept called runtime-type-information. I expect that
> the 
> dynamic_cast<vtkBaseClass>(vtkParentClass)
> operator would do the job. But the vtk class library seems
> to make no use of it. Does anyone know the reason for
> this?
That feature is subject of many guru-discussions, because many
programmers believe it is bad habit to write code that depends upon the
exact (run-time) type of objects. It is like breaking away the legs from
underneath the concept of Object Oriented Programming. Besides, not all
compilers implement it the same way (if at all).

But you can use a cast to access the base class
(static_cast<vtkBaseClass*>(pointer_to_class)), but that's only to
inform the compiler. If you want to call an implementation of a function
that is redefined in a derived class, you need the scope operator:
this->vtkBaseClass::member_function(...)
Either way, you need to know at /compile time/ what type the base class
is. Not run-time.

> If I would use it, I had to recompile the library to activate
> runtime type information, but then I am not sure if there
> are any counter-arguments to using this feature.
> 
> Also since this duplicates/interferes with a standard convention
> in the vtk library I would like to use standard conventions
> as much as possible.
> 
> Is there any development going on in this direction?
> (a pointer to the parent class would be fine too.)
> 
What I still don't understand is why you'd want to figure it out at
run-time. What is your application? Are you sure there is no better way?
To speak with Jeff Alger (author of "Secrets of the C++ masters"):
"If you are ever tempted to do something like this, get up from your
chair, walk slowly around the block a few times, take a deep breath,
then sit down at your desk and choose a different design."

> Thank you,
> 
You're welcome.

> Roland Schwarz
> Business Email: RSchwarz at riegl.co.at
> Private Email: roland.schwarz at telecom.at
> 
> 
> ir. Marc Schrijver
> Faculty of Electrical Engineering (S&S NT)
> University of Twente
> The Netherlands
> 
> 


-----------------------------------------------------------------------------
This is the private VTK discussion list.  Please keep messages on-topic.
Check the FAQ at: <http://www.automatrix.com/cgi-bin/vtkfaq>
To UNSUBSCRIBE, send message body containing "unsubscribe vtkusers" to
<majordomo at gsao.med.ge.com>.  For help, send message body containing
"info vtkusers" to the same address.     Live long and prosper.
-----------------------------------------------------------------------------





More information about the vtkusers mailing list