Deriving from VTK classes?
Roland Schwarz
roland.schwarz at telecom.at
Fri Sep 17 11:29:46 EDT 1999
Hello Dean,
thank you for having answered. I already have heard some
of the things you told me. But we aren't living in an ideal world,
do we?
>The complete hierarchy is fixed at compile-time! So, you don't have to
>figure out the parent; you simply KNOW it.
True, but if I have a pointer to a class which is a descendant of the parent
which was created later in the design cycle, the COMILER KNOWS its type.
But I did NOT KNOW of the descendant when I wrote the code that works
on this pointer. Well I hear you say
>...
>So, you don't "know" which implementation is
>called ..... That's exactly the beauty of it!
>
This is true, and what I should do is to extend the root of the class
hierarchy to contain a virtual function, that gets called in every
descendant. BUT my root class is a vtk Library class, and in some
descendant I implemented a (virtual) function that does something
meaningful. Still I want to be able others to derive from my new class.
Moreover the pointer to my derived class is handed me over from
the vtk Library (which does not know anything about my implementation-
and should not need to know).
So I get a pointer, and depending wether it is my derived class or a more
basic class I want my code to work. Ok, if it is my class I can query
GetClassName, but what if is an other name? Is it safe to call the function?
This depends on its position in the class hierarchy. This exactly is what
dynamic_cast is for.
In an ideal world I would modify the vtk Library, but I definitely do NOT
want
to do this. I am not trying to open Pandoras Box anyway.
In other words:
class vtkWindow
{
...
virtual int* GetSize();
...
}
class myCoolWindow : public vtkWindow
{
...
virtual int* GetOrientation();
...
}
....
vtkWindow* window = this->VTKWindow;
int* dir = window->GetOrientation(); // oops not in vtkWindow
int* dir = ((myCoolWindow*)window)->GetOrientation(); // oops could be a
vtkWindow
...
Perhaps I did not >>walk slowly around the block a few times<< often enough,
but I can see no good design at all. Perhaps you can give me a hint. I think
the
use of any library will exhibit similar problems. And I always thought this
was one
of the reasons why object oriented programming has been invented: You should
be able to customize code that has already been written.
If you can see the light please tell me.
Have a nice day,
Roland Schwarz
-----------------------------------------------------------------------------
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