Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C+的运算符重载+;:使用指针datat成员初始化_C++_Oop_Operator Overloading_Dynamic Memory Allocation - Fatal编程技术网

C++ 使用C+的运算符重载+;:使用指针datat成员初始化

C++ 使用C+的运算符重载+;:使用指针datat成员初始化,c++,oop,operator-overloading,dynamic-memory-allocation,C++,Oop,Operator Overloading,Dynamic Memory Allocation,我正在尝试一个使用运算符重载执行基本矩阵运算的程序。下面是我的代码:- 保持矩阵的类 class matrix { int r, c; int *data; public : matrix() //default contstructor { r=0; c=0; data = NULL; } matrix(int a, int b) //general matrix constructor.

我正在尝试一个使用运算符重载执行基本矩阵运算的程序。下面是我的代码:-

保持矩阵的类

class matrix {
    int r, c;
    int *data;

    public :
    matrix()  //default contstructor
    {
        r=0; c=0;
        data = NULL;
    }

    matrix(int a, int b)   //general matrix constructor.
    {
        r = a; c = b;
        data = new int[sizeof(int) * r * c];;
    }

    //Overloading + operator.
    matrix operator +(matrix & M);

             //Overloading = operator.
    void operator =(matrix & M);
};
然后我创建了一个临时全局对象,如下所示

matrix temp;
我重载了+运算符,如下所示。注意,我必须使用上面创建的全局对象“temp”来保存和返回结果矩阵,因为我的数据成员是int*并且我不能返回具有本地范围的对象

// Addition of matrix
matrix matrix :: operator+(matrix & M)
{
         if(M.r != r || M.c != c) {
           cout<<"Addition is not possible!";
           return temp;
         }
         temp.r = r;
         temp.c = c;
         temp.data = new int(sizeof(int) * r * c);

         for(int i=0; i<r; i++)
         for(int j=0; j<c; j++)
          *(temp.data +(i*c +j)) = *(data +(i*c +j)) + *(M.data +(i*c +j));

     return temp;
}
//矩阵的加法
矩阵::运算符+(矩阵和M)
{
如果(M.r!=r | | M.c!=c){
库特
这个外部“临时”对象是否有有效的替代方案

是的(代码中也有一些问题)

矩阵和应通过两种方式实现:

class matrix {
    // ...

    //Overloading + operator.
    matrix& operator +=(const matrix& M); // << notice difference in signature
};

// Addition of matrix
matrix& matrix::operator +=(const matrix& M)
{
     if(M.r != r || M.c != c) {
         cout<<"Addition is not possible!"; // this should throw an exception
                                            // because client code should not have to
                                            // be forced to check the contents of
                                            // std::cout to validate that the operation
                                            // succeeded

         return *this;                      // returning *this
     }

     // this is not necessary
     // temp.r = r;
     // temp.c = c;
     // temp.data = new int(sizeof(int) * r * c);

     for(int i=0; i<r; i++)
         for(int j=0; j<c; j++)
             *(data +(i*c +j)) = *(data +(i*c +j)) + *(M.data +(i*c +j));

     return *this;                          // returning *this
}

第二个实现使用第一个实现为矩阵添加加法语义,它不需要是成员。

极有可能省略
temp
副本,因此您应该担心的唯一低效之处是当您有
m0+m1+m2+m2
等表达式时,
mi
都是矩阵这个问题的解决方案是,但它很棘手。旁白:运算符应该是
const
,并根据
const
参考:
矩阵运算符+(const-matrix&M)const;
,数组的大小看起来可疑。为什么要将临时对象声明为全局对象?只需将其声明为函数中的局部变量即可。@JoachimPileborg所说的。我所说的复制省略假设
temp
是运算符中的局部变量。生成行和列参数是很常见的模板参数。例如
矩阵
。这使得将
矩阵
添加到
矩阵
中成为编译时错误。请注意,如果您已经编写了
操作符+=
,您不必自己编写
操作符+
,使用或我的库,第二个允许优化表达式中的一些临时值,如
auto result=m1+m2+m3+m4;
。我知道,但OP正在研究定义加法运算符的正确签名和语义。向示例中添加第三方库、C++11允许的优化和/或继承、模板和CRTP有点为时过早。我本可以对代码添加更多的改进和建议(例如,使用std::vector)但我想重点关注函数签名和(非常基本的)正确实现。
matrix operator +(const matrix& x, const matrix& y) {
    matrix result(x); // you will need a copy constructor
    result += y;      // use operator defined above
    return result;
}