我有一个基类命名的变量基类功能:重写与不同的返回类型
class Variable
{
protected:
std::string name;
public:
Variable(std::string name="");
Variable (const Variable& other);
virtual ~Variable(){};
};
我有几个派生类,如int,布尔,字符串等。例如:
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();
但它没有帮助。
有什么建议吗?我必须使用铸造吗?
非常感谢......
显示您在基座尝试什么和派生类 –
'可变条件= condition_statement.evaluate(类型);' //其中'condition_statement.evaluate(类型)'返回一个布尔对象 'COUT << condition.getval ();' 编译器说:“'类变量'没有名为'getVal'的成员” – Shakedk
请注意,您只能拥有**协变返回类型**,否则您会隐藏基类函数,这不是你想要的行为。 –