简体   繁体   English

如何重载 C++ 运算符以添加双加<matrix> ?

[英]How can I overload a C++ operator to add double plus <matrix>?

So, I'm fairly new to C++ but I have done lots of C programming.所以,我对 C++ 相当陌生,但我已经完成了很多 C 编程。 Partly as an exercise, and also partly because I don't have a matrix package that permits the fine-grained control over things that I would like, I have been writing my own matrix class mtMatrix and overloading various operators to let me write equations conveniently.部分是作为练习,部分是因为我没有允许对我想要的东西进行细粒度控制的矩阵包,我一直在编写我自己的矩阵类 mtMatrix 并重载各种运算符让我方便地编写方程. I've overloaded +,-,/, and * to perform element-wise arithmetic operations, after checking that the matrices are of the same size, while full-on matrix multiplication is done by the class method Mult().在检查矩阵大小相同之后,我重载了 +、-、/ 和 * 以执行逐元素算术运算,而完整的矩阵乘法是通过类方法 Mult() 完成的。 I've further overloaded +,-,/, and * to multiply matrices by scalars, so I can have a mtMatrix M and write "mtMatrix T = M * 2.0" to get a new matrix whose entries are twice as large as the old one.我进一步重载了 +、-、/ 和 * 以将矩阵乘以标量,所以我可以有一个 mtMatrix M 并编写“mtMatrix T = M * 2.0”来获得一个新矩阵,其条目是旧矩阵的两倍一。 However, when I try to write "mtMatrix T = 2.0 * M" I run into problems, and I'm not sure how I would write the overloading for +,-, and * to accomplish this, even though what I would want is precisely the same result as if I were to type "mtMatrix T = M * 2.0."但是,当我尝试编写“mtMatrix T = 2.0 * M”时遇到了问题,我不确定如何编写 +、- 和 * 的重载来实现这一点,即使我想要的是结果与我键入“mtMatrix T = M * 2.0”完全相同。 It seems that the contents of the overloading function would be identical in the two cases, but the signature of the function would be different.两种情况下重载函数的内容似乎是相同的,但函数的签名会有所不同。 Can anyone point me in the right direction?任何人都可以指出我正确的方向吗?

You need to create an operator associated with an lvalue as int.您需要创建一个与左值关联的运算符作为 int。 The operator* you have declared in your class has as lvalue 'this' therefore M*2.0 works.您在类中声明的 operator* 具有左值“this”,因此 M*2.0 有效。

In order to create an operator associated as lvalue for int you need to declare it outside the class, and if the operator has as rvalue an object of your class then you need to declare it as friend so it can access the private values.为了创建一个与 int 左值关联的运算符,您需要在类之外声明它,如果运算符将类的对象作为右值,那么您需要将其声明为友元,以便它可以访问私有值。

Below is an example, where the class in not a matrix but simulates the behavior you want to accomplish.下面是一个示例,其中的类不是矩阵,而是模拟您想要完成的行为。


#include <iostream>

class M 
{
    int value_;

    public:
    M(int value):value_(value){}
    int& operator()() { return this->value_; }
    int operator()() const { return this->value_; }
    int operator*(int value) { return this->value_ * value; }

    friend int operator*(int, const M&) ;  //so it can access the M private objects
};

int operator*(int value, const M& other) 
{
    return value * other.value_;
}

int main()
{
    M m0(10);
    M m1(5);
    m0() = m0() * 10;
    std::cout << m0() << std::endl;

    m0() = 10 * m1();
    std::cout << m0() << std::endl;

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM