c++ - Is it possible to create a container that holds one of three data types, and can be queried through a single interface? -
i have container object holds either int, float or string. have queue of these containers processed in similar way. @ moment have separate getters different data types. isn't particularly elegant, given each container instance hold 1 data type @ time.
class mycontainer { mycontainer(float value); mycontainer(int value); mycontainer(string value); int getintvalue(); float getfloatvalue(); string getstringvalue(); } void processcontainer(mycontainer& container) { // following not work, desired: process(container->getvalue()); // compilation error } void process(int value) {} void process(float value) {} void process(string value) {}
is there way can exploit parameter overloading of process method above? example, way of getting point can call process(container->getvalue())?
you can use templates , traits restrict templated code types string, float, int.
Comments
Post a Comment