I’m trying to figure out the best approach for a c++ program:
I need to make an object called Characteristic. This guy has 4 attributes:
– String name (just the name)
– ? type (what type of characteristic it is. Can be either numeric or descriptive)
– ? range (numeric – the min and max
descriptive – the options)
– ? value (the actual chosen value could be int or string depending on the type)
If I was in Java I would create an object Type with two children: Numeric and Descriptive. Each would have the appropriate range and store the value in the appropriate format.
Examples for both:
Name: Warmeness
Type: Numeric
Range: min 1 max 5
Value: 2
Name: Style
Type: Descriptive
Range: minimalistic photography
Value: minimalistic
I have no idea what is the best way to do this in c++.
Should I be looking at templates? Because if so I cannot figure out how to use them.
You can do the same thing as Java if you want.
However before you can decide how to implement your Characteristic you have to better define how you’re going to use it and the interface you want. For example the above doesn’t make it easy to use to use a Type object as a polymorphic object. You’d have to manually check if a Type pointer points at a Numeric or Descriptive and act accordingly.
One way to get polymorphism is C++ is to use virtual methods:
Now when you call
do_somethingon aType *it will figure out the right method for the dynamic type and call that.