【发布时间】:2021-10-12 11:08:57
【问题描述】:
我目前正在深入研究 C++。我有一个多态性/继承问题,我无法弄清楚,也没有找到(对我而言)似乎可以解决我的具体问题的在线答案:
我想要一个基类和几个派生子类。我想在子类访问的基类中定义通用函数/运算符重载,以避免代码重复。
这是我想弄清楚的例子:
class ElectricalUnit {
protected:
float value;
public:
ElectricalUnit(float _value) : value(_value) {};
float getValue() const {
return this->value;
}
// this should be the (only) place where the add operation for all subclasses is defined
// note: I intentionally want to avoid changing the objects' state by returning a new object
ElectricalUnit operator+ (const ElectricalUnit& other) const {
return ElectricalUnit(this->value + other.getValue());
}
};
class Voltage : public ElectricalUnit {
public:
Voltage(float _voltage) : ElectricalUnit(_voltage) {};
float getVoltage() const {
return ElectricalUnit::getValue();
}
Voltage operator+ (const Voltage& other) { // not having this function did not work
// 1st try:
return ElectricalUnit::operator+(other); // error: says no conversion is available??
// 2nd try:
return (Voltage)ElectricalUnit::operator+(other); // error: also says no conversion is available.
// 3rd try (read it online)
ElectricalUnit::operator+(other);
return *this; // does give wrong values, because the return value of operator+ is not used
}
};
class Current : public ElectricalUnit {
private:
float current;
public:
Current(float current) : current(current) {};
float getCurrent() { return this->current; };
/* use operator+ method from base class --> but how? */
};
这里,Voltage 和 Current 派生自基类 ElectricalUnit。在每个子类中添加电压或电流(或其他,如功率、能量等)都是相同的。所以在ElectricalUnit 中我定义了运算符重载方法,并希望子类使用这个方法。在考虑公差等时,操作可能会变得更加复杂。我的目标是只有一种实现逻辑的方法。
这是一个示例程序:
int main() {
Voltage u1 = { 4 };
Voltage u2 = { 6 };
Voltage u3 = u1 + u2;
Voltage u4 = u3 + u2;
std::cout << "Voltage U1 = " << u1.getVoltage() << " V" << std::endl;
std::cout << "Voltage U2 = " << u2.getVoltage() << " V" << std::endl;
std::cout << "Voltage U3 = " << u3.getVoltage() << " V" << std::endl;
std::cout << "Voltage U4 = " << u4.getVoltage() << " V" << std::endl;
return 0;
}
我是如何做到这一点的,还是我误会了什么?我是一名 Python 程序员,完全不考虑类型就可以工作^^
我很高兴每一个帮助:) 谢谢。
【问题讨论】:
-
在基类中实现的
operator+重载返回一个ElectricalUnit对象。而已。即使此EletricalUnit是Voltage的子类,此operator+重载仍会返回ElectricalUnit。撇开在派生类中使用重载的问题不谈,这就是 C++ 的工作方式,没有办法改变它。 -
继承对于这项工作来说是一个错误的工具。您可能想研究实现此功能的库,例如boost.units。不幸的是,我可以在这里描述它的复杂性大约是 1e6 倍,而且有很多充分的理由。
-
我是一名 Python 程序员,完全不考虑类型 -- 你现在已经看到 Python 不是 C++,而且使用 Python(或任何其他语言)C++ 中的技术将无法正常工作。 C++ 是目前最复杂的语言之一,如果你试图在 C++ 中结合其他语言的习语和技术,你将无法快速取得进展。 C++ 是基于值的,而 Python 和其他语言是基于引用的,这一事实已经让你陷入了困境。正确的做法是忘记 Python 的存在,正确学习 C++,并将 C++ 用作 C++。
标签: c++ inheritance operator-overloading subclass