design question: templates or inheritance?

M

Markus Seeger

Hi,

I'm writing a commandline argument parser that can handle switches
by using some techniques similar to the Qt slot mechanism.

example:
// inheritance method (what I'm trying at the moment)
parser.setSwitch(BoolSwitch("v", "verbose", &bVerbose));
parser.setSwitch(GeometrySwitch("s", "size", &sizeGeometry));

// template method
parser.setSwitch(Switch<bool>("v", "verbose", &bVerbose));
parser.setSwitch(Switch<Geometry>("s", "size", &sizeGeometry));

call:
char **argv = {"./example", "--verbose", "-s", "800x600"};
parser.parseCommandLine(4, argv);

Internally I store the Switches in a map using Switch * base pointers.
The appropriate method is called later using dynamic binding.

Q:
So if I'm going to use templates, can I create a similar anonymous
Switch * array? I can't use Switch<someType> * because I don't know
the specific type at definition time...

Somehow templates seem more elegant to use here. Perhaps it's just
my urge to finally start using templates somewhere ;)

Q:
In the case of non-atomic types like Geometry (2 params), I am in
need of a generic parse method... using inheritance it's just overriding,
but what to do with templates? Is this a k.o.-criteria for templates?

thx in advance, Markus
 
M

Markus Seeger

Markus said:
Q:
In the case of non-atomic types like Geometry (2 params), I am in
need of a generic parse method... using inheritance it's just overriding,
but what to do with templates? Is this a k.o.-criteria for templates?

If this second one is a pattern question and doesn't belong here, just
ignore it ;D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top