我有一个包含 int、float 或 string 的容器对象。我有一个以类似方式处理的这些容器的队列。目前我对不同的数据类型有单独的吸气剂。这并不是特别优雅,因为每个容器实例一次只能保存一种数据类型。
class MyContainer
{
MyContainer(float value);
MyContainer(int value);
MyContainer(string value);
int getIntValue();
float getFloatValue();
string getStringValue();
}
void processContainer(MyContainer& container)
{
// the following will not work, but is desired:
process(container->getValue()); // compilation error
}
void process(int value) {}
void process(float value) {}
void process(string value) {}
有没有办法可以利用上述过程方法的参数重载?例如,某种方式可以让我简单地调用 process(container->getValue())?