|
|
In article <3c4ffa14@news.povray.org>, Warp <war### [at] tagpovrayorg>
wrote:
My application is a finite state machine (I guess it's a virtual machine
of the type used by Java), and relies on data of various types being
placed on a single stack. I've implemented a data wrapper class,
subclasses of which contain the actual data. The subclasses "own" the
data they contain, they allocate it and delete it, and have a secondary
purpose of implementing reference counting so several symbols can exist
for the same data. However, I sometimes need to copy them...the stack
only has base class pointers, and the code for copying them doesn't care
what type they are, only that they are data that needs to be copied, so
a virtual Copy() method is the logical choice. The data wrapper classes
still take care of allocation and deallocation, the outside world only
has to keep track of the wrappers.
> It's always better to do "downcasting" with a virtual function call instead
> of doing it "by hand".
Unfortunately, a function with a different return type hides the
original instead of overriding it, so casting is necessary. The code
usually knows by context what type it is, and sometimes uses
dynamic_cast when there are several possibilities. Virtual functions are
used when possible.
I could make functions for every possible data type and put them all in
the base class, overriding specific ones for the subclasses, but I don't
think anyone would claim that's good design.
--
--
Christopher James Huff <chr### [at] maccom>
Post a reply to this message
|
|