Overload =操作符,类应该像矩阵行为 [英] Overload = operator for class that shall behave like matrix

查看:198
本文介绍了Overload =操作符,类应该像矩阵行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类的模板,应该像矩阵行为。
所以usecase是这样的:

 矩阵(10,10) 
matrix [0] [0] = 4;
//设置矩阵其余部分的值
cout<< matrix [1] [2]<< endl;

当我直接在构造函数中设置值时,效果很好,但是当我想使用 matrix [x] [y] = z; 我得到错误:需要作为赋值的左操作数的lvalue。我假设,我必须重载 = 运算符。然而,我试了整晚,我没有找到,如何实现它。请任何人请这么亲切,并告诉我如何为我的代码重载 = 运算符,以便为该矩阵赋值?



代码:

  #include< iostream> 
#include< cstdlib>
#include< cstdio>
#include< cstring>
#include< sstream>

using namespace std;

class Matrix {
public:

Matrix(int x,int y){
_arrayofarrays = new int * [x]
for(int i = 0; i _arrayofarrays [i] = new int [y]

//在这里工作
_arrayofarrays [3] [4] = 5;
}

class Proxy {
public:

Proxy(int * _array):_array(_array){
}

int operator [](int index){
return _array [index];
}
private:
int * _array;
};

代理运算符[](int index){
return代理(_arrayofarrays [index]);
}

private:
int ** _arrayofarrays;
};

int main(){
矩阵(5,5);

//不起作用:-S
// matrix [2] [1] = 0;

cout<< matrix [3] [4]< endl;
}


解决方案

如果您打算修改元素,那么代理类中的 operator [] 的重载必须返回一个引用:

  int&  int <$> 

<$> <$> <$> ,这使得一个元素的值的副本 - 不是你想要的。应该有一个 const 重载,以便 operator [] const 矩阵。这可以通过值返回:

  int operator [](int index)const 

其实, size_t 会比 int ,因为它是一个无符号类型。



您不需要重载 operator = ,除非您想一次性分配整行。事实上,你根本不需要 Proxy 类,因为你可以直接返回一个指向行数组的指针。然而,如果你想改变你的设计 - 例如,使用稀疏或打包的表示,然后代理将让你保持 m [i] [j] 接口。


I've got a template of a class that shall behave like matrix. So the usecase is something like:

Matrix matrix(10,10);
matrix[0][0]=4;
//set the values for the rest of the matrix
cout<<matrix[1][2]<<endl;

When I set the values directly in the constructor, it works well, but when I want to use matrix[x][y]=z; I get error: lvalue required as left operand of assignment. I assume, that I must overload = operator. Nevertheless I tried whole evening and I didn't find out, how to implement it. Would anybody be please so kind and show me how to overload = operator for my code, to make it assign values to that matrix?

code:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <sstream>

using namespace std;

class Matrix {
public:

    Matrix(int x,int y) {
        _arrayofarrays = new int*[x];
        for (int i = 0; i < x; ++i)
            _arrayofarrays[i] = new int[y];

        // works here
        _arrayofarrays[3][4] = 5;
    }

    class Proxy {
    public:

        Proxy(int* _array) : _array(_array) {
        }

        int operator[](int index) {
            return _array[index];
        }
    private:
        int* _array;
    };

    Proxy operator[](int index) {
        return Proxy(_arrayofarrays[index]);
    }

private:
    int** _arrayofarrays;
};

int main() {
    Matrix matrix(5,5);

    // doesn't work :-S
    // matrix[2][1]=0;

    cout << matrix[3][4] << endl;
}

解决方案

If you intend to modify the element of the matrix referenced by the proxy, then the overload of operator[] in the Proxy class must return a reference:

int& operator[](int index)

At the moment, you return int, which makes a copy of the element’s value—not what you want. There ought to be a const overload as well, so that operator[] works on const matrices. This one can return by value:

int operator[](int index) const

And actually, size_t would be more appropriate for the index than int, since it’s an unsigned type. You aren’t giving any particular meaning to negative indices, so it makes sense to disallow them.

You don’t need to overload operator= of Proxy unless you want to assign a whole row at once. In fact, you don’t need the Proxy class at all, because you can just return a pointer to the row array directly. However, if you want to change your design—e.g., using a sparse or packed representation—then the Proxy would let you keep the m[i][j] interface.

这篇关于Overload =操作符,类应该像矩阵行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆