Re: How to determine the type of data at run time?
Larry Evans wrote:
On Sep 21, 11:29 pm, Dragan Milenkovic <dra...@plusplus.rs> wrote:
On 09/16/2010 06:54 PM, Matthew Collett wrote:
This is a textbook application of polymorphism.
Define an abstract base class, let's say MatBase, which contains a Mat
(or a pointer to one). For eachdatatype, define a derived class:
IntMat, FloatMat, etc.
[...]
Actually, I would call this a textbook anti-application of polymorphism.
[snip]
Others have already suggested using a variant and a visitor.
[snip]
[...]
and that uses polymorphism; hence, I'm not sure it is an example
of an anti-application. Could you further justify this conclusion
in light of the Visitor_pattern?
I'd say that trying to coerce different datatypes into a common interface,
so that you can handle them by just using the interface to the baseclass is
what is wrong. This leads to bloat in the interface (the baseclass needs
everything and the kitchensink) while the implementations become spare and
typically throw a "not supported" exceptions for most functions because
they can't sensibly implement them.
The visitor pattern (a.k.a. double dispatch) is typically implemented using
a single virtual function, so it indeed uses polymorphism. The difference
is that the actual handling is defined by the visitor, and that is supplied
by the code calling the code, not by the implementation of the the actual
types that are being visited. That also means that this caller can make a
decision how it wants to handle different situations and it also avoids the
interface bloat and sparse implementation.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]