c++ - 运算符重载使用 * 和 *= 作为点积或叉积

标签 c++ vector

<分区>

我正在为基本 vector 运算创建一个 vector 类,并试图决定哪个 [点积或叉积] 最适合重载 **=运算符并愿意了解其他人的想法。

到目前为止,我有 + , += , - , 和 -=如此实现。

template<typename T>
Vector<T> Vector<T>::operator+(const Vector<T>& rhs) {
    Vector<T> result(*this);
    result += rhs;
    return result;
}

template<typename T>
Vector<T>& Vector<T>::operator+=(const Vector<T>& rhs) {
    for (int i = 0; i < this->size; i++)
        this->vector[i] += rhs[i];

    return *this;
}

template<typename T>
Vector<T> Vector<T>::operator-(const Vector<T>& rhs) {
    Vector<T> result(*this);
    result -= rhs;
    return result;
}

template<typename T>
Vector<T>& Vector<T>::operator-=(const Vector<T>& rhs) {
    for (int i = 0; i < this->size; i++)
        this->vector[i] -= rhs[i];

    return *this;
}

继续回归的趋势Vector<T> * 应该使用叉积吗?和 *=运算符重载或返回 T&使用点积。

你有什么想法?

最佳答案

如果您想知道 * 应该表示哪个操作,这一事实强烈暗示您不要使用它。事实上,**= 也可以表示分量乘法。避免混淆是这里的最佳选择,因此只需使用名称 dotcross 进行操作,而忘记 * 运算符。

关于c++ - 运算符重载使用 * 和 *= 作为点积或叉积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25394283/

相关文章:

c++ - 我如何使用中点圆算法去除在 SDL 中绘制的圆中的这些间隙?

c++ - 具有奇怪行为的结构 vector C++

c++ - 我需要声明一个包含整数 vector 的 4x4 矩阵

c++ - 如何检查数字序列是否在C++中具有增加/减少趋势

c++ - 查找数字中特定的重复数字

c++ - GLEW 和 GLFW .dylib 文件缺少 MAC

c++ - 将通用文件包含在一个头文件中有什么好处?

c++ - cv::perspectiveTransform 断言失败

C++计算数字(不是字符串)之间的空格

c++ - 替换 vector 字符串中的字符串