Re: Convert case/if's code to Polymorphism code
On Oct 1, 8:32 am, ThosRTanner <ttann...@bloomberg.net> wrote:
On Oct 1, 9:26 am, Vizcayno <vizcay...@gmail.com> wrote:
I have the next C++ code:
if (type == "date_only")
return convert_date(data);
else if (type == "date_time")
return convert_date_time(data);
else if (type == "max_double")
return convert_max_double(data);
else if (type == "amount")
return convert_max_double(data);
else ...
How can I apply polymorphism to this construction? Could you help me
coding my example in "polymorphism mode", please?
It'd be nice to know what you are converting to and from. Then it
might be possible to be more specific.
normally you'd have a base class which had a virtual convert method,
and various classes overriding this method, but I've seen similar done
with templates as well.
YMMV, and there's not enough of a route plan here to tell by how much
{ edits: quoted banner removed. please don't quote extreneous material. -mod }
Hello, thanks for your answer. Every function invoked in my example
has many and different operations, some of
them return string, others double. I want to avoid the use of if/else
or switch-case, the same quantity of if/else is repeated in other
places into my code. I read ways to use polymorphism but none of them
seems to apply to my case.
Is this possibble to help me with some examples? or at least indicate
me where can I find something related?
Thanks!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]