I'm building a class which I want to configure using various parameters which may one of: int
, double
and string
(or const char *
for that matter). In a language like Ruby, I would build an initialization function that takes a hash keyed by a string. For example:
class Example
def init_with_params params
puts params
end
end
e = Example.new
e.init_with_params({ "OutputFile" => "/tmp/out.txt", "CaptureFPS" => 25.0, "RetryDelaySeconds" => 5 })
How can I create a similar behavior in C++?
Looking around I found several posts talking about boost::variant
. I would rather avoid using boost if by limiting the different types to the 3 types I mentioned above a rather clean solution can be made.
Edit: I agree that using a well designed and widely tested code such as Boost.Variant is much better than re-implementing the same idea. In this case, the problem is reduced to using only 3 basic types, so I was looking for the simplest possible way of implementing it.