我有一个名为变量的基类:
class Variable
{
protected:
std::string name;
public:
Variable(std::string name="");
Variable (const Variable& other);
virtual ~Variable(){};
};
我有几个派生类,例如 Int、Bool、String 等。例如:
class Bool: public Variable{
private:
bool value;
public:
Bool(std::string name, bool num);
~Bool();
Bool (const Bool& other);
bool getVal();
每个派生类都有一个名为 getVal() 的方法,该方法返回不同的类型(bool、int 等)。我想允许变量类的多态行为。
我试过:void getVal();
这似乎是错误的,编译器显示错误:shadows Variable::getVal()
这听起来很糟糕。我想过使用template <typename T> T getVal();
,但它没有帮助。
有什么建议么?我必须为此使用强制转换吗?
非常感谢...