制作 HyperComplex 时 operator+ 优先级的问题 class

problem with operator+ priority in making a HyperComplex class

我正在制作一个 HyperComplex class 只是为了练习,我在 2 个 HyperComplex 变量之间的 operator+ 行为有问题,我得到了常规的复杂 operator+, 我也尝试做 2 个 opeartor+(第一个是 Hc 和 Complex,第二个是 complex 和 Hc,但这给了我模棱两可的 eroor)

这是我的代码:

class Complex {
public:
 Complex(const T& real = 0, const T& imaginary = 0);
 Complex(const Complex&) = default;
 virtual ~Complex() {};
 Complex& operator=(const Complex&) = default;
 Complex& operator+=(const Complex&);
 }
protected:
 T real;
 T imaginary;
};

template <class T>
Complex<T>& Complex<T>::operator+=(const Complex<T>& rhs){
    real += rhs.real;
    imaginary += rhs.imaginary;
    return *this;
}

template <class T>
Complex<T> operator+(const Complex<T>& lhs, const Complex<T>& rhs){
    Complex<T> tmp = lhs;
    return tmp += rhs; 
}

template <class T>
class HyperComplex: public Complex<T> {
    private:
    T j_part;
    public :
    HyperComplex(const T& real = 0, const T& imaginary = 0, const T& j_part = 0): Complex<T>(real,imaginary), j_part(j_part) {}    
    HyperComplex(const HyperComplex&) = default;                                      
    HyperComplex(const Complex<T>& p): Complex<T>(p) , j_part(0) {
        const HyperComplex* ptr = dynamic_cast<const HyperComplex*>(&p);
        if (ptr != nullptr){
            j_part = ptr->j_part;
        }
    }    // copy ctor gets ref to complex
    HyperComplex<T>& operator+=(const HyperComplex<T>& rhs){
        this->real += rhs.real;
        this->imaginary += rhs.imaginary;
        j_part += rhs.j_part;
        return *this;
    }

};

template <class T>
HyperComplex<T> operator+(const HyperComplex<T>& lhs, const Complex<T>& rhs){
    HyperComplex<T> tmp = lhs;
    return tmp += rhs; 
}

template <class T>
HyperComplex<T> operator+(const Complex<T>& lhs, const HyperComplex<T>& rhs){
    HyperComplex<T> tmp = rhs;
    return tmp += lhs; 
}

当我 运行 此代码时:

   HyperComplex<int> hc(4,2,4) ;
    HyperComplex<int> hc1 = hc + hc;
    HyperComplex<int> hc2 = c2;
    HyperComplex<int> hc3 = hc + c2;
    HyperComplex<int> hc4 = c2 + hc;

我在 hc1 计算中变得模棱两可

对于初学者来说,class 模板 Complex 的析构函数应该是虚拟的

virtual ~Complex() = default;

制作这个构造函数

HyperComplex(const Complex<T>& p);

明确

explicit HyperComplex(const Complex<T>& p);

否则 Complex 类型的对象可以隐式转换为 HyperComplex 类型的对象,反之亦然。

结果是运算符

template <class T>
Complex<T> operator+(const Complex<T>& lhs, const Complex<T>& rhs){
    Complex<T> tmp = lhs;
    return tmp += rhs; 
}

或运算符

template <class T>
HyperComplex<T> operator+(const HyperComplex<T>& lhs, const HyperComplex<T>& rhs){
    HyperComplex<T> tmp = lhs;
    return tmp += rhs; 
}

可以在这些语句中调用

HyperComplex<int> hc3 = hc + c2;
HyperComplex<int> hc4 = c2 + hc;

另一种方法是显式声明另外两个运算符,例如

template <class T>
HyperComplex<T> operator+(const HyperComplex<T>& lhs, const Complex<T>& rhs);

template <class T>
HyperComplex<T> operator+(const Complex<T>& lhs, const HyperComplex<T>& rhs);

但是在任何情况下,class带有默认参数的 es 的构造函数都应该声明为显式的。

问题已解决!解决方案是 4 个不同的 operator+ :

Complex<T> operator+(const Complex<T>& lhs, const Complex<T>& rhs){
    Complex<T> tmp = lhs;
    return tmp += rhs; 
}

template <class T>
HyperComplex<T> operator+(const HyperComplex<T>& lhs, const Complex<T>& rhs){
    HyperComplex<T> tmp = lhs;
    return tmp += rhs; 
}

template <class T>
HyperComplex<T> operator+(const Complex<T>& lhs, const HyperComplex<T>& rhs){
    HyperComplex<T> tmp = rhs;
    return tmp += lhs; 
}

template <class T>
HyperComplex<T> operator+(const HyperComplex<T>& lhs, const HyperComplex<T>& rhs){
    HyperComplex<T> tmp = rhs;
    return tmp += lhs; 
}