运算符重载 [][] 二维数组 C++ [英] operator overloading [][] 2d array c++

查看:41
本文介绍了运算符重载 [][] 二维数组 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组,我想定义一个函数,该函数返回用户使用运算符重载给我的索引值.换句话说:

void MyMatrix::ReturnValue(){整数行 = 0,列 = 0;cout<<来自最后一个矩阵的返回值"<<结束;cout<<-----------------------------------"<<结束;cout<<"请输入索引:["<<行<<"]["<<颜色<<] ="<<((*this).matrix)[row][col] <<结束;}

操作 ((*this).matrix)[row][col] 应该返回一个 int.我不知道如何构建 operator [][].
或者,我可以连接对 operator [] 的几次调用,但我没有成功,因为对该操作符的第一次调用将返回 int* 和第二个将返回 int,它会强制构建另一个运算符,而我不想这样做.

数据矩阵定义如下

int** 矩阵;矩阵 = 新 int*[row];如果(矩阵 == NULL){cout<<分配内存 - 失败";}for (int i = 0; i < row; i++)//分配内存{矩阵[i] = new int[col];如果(矩阵[i] == NULL){cout<<分配内存 - 失败";返回;}}

我能做什么?谢谢,

解决方案

简单来说,这样的运算符不存在,所以你不能重载它.

一个可能的解决方案是定义两个类:MatrixRow.
您可以定义 Matrixoperator[] 使其返回 Row,然后为 Row<定义相同的运算符/code> 以便它返回一个实际值(int 或任何你想要的,你的 Matrix 也可以是一个模板).
这样,语句 myMatrix[row][col] 将是合法且有意义的.

为了将新的 Row 分配给 Matrix 或更改 Row 中的值,也可以这样做.>

* 编辑 *

正如评论中所建议的,您也应该考虑 在这种情况下使用 operator() 而不是 operator[].
这样,也不再需要 Row 类.

I have a 2D array and I want to define a function that returns the value of the index that the user gives me using operator overloading. In other words:

void MyMatrix::ReturnValue()
{
    int row = 0, col = 0;
    cout << "Return Value From the last Matrix" << endl;
    cout << "----------------------------------" << endl;
    cout << "Please Enter the index: [" << row << "][" << col << "] =" << ((*this).matrix)[row][col] << endl;
}

The operation ((*this).matrix)[row][col] should return an int. I have no idea how to build the operator [][].
Alternatively, I could concatenate a couple of calls to the operator [], but I didn't succeed in it, because the first call to that operaror will return int* and the second one will return int, and it compel to build another operator, and I dont want to do that.

The data matrix is defined like

int** matrix; matrix = new int*[row];
if (matrix == NULL)
{
    cout << "Allocation memory - Failed";
}
for (int i = 0; i < row; i++)//Allocation memory
{
    matrix[i] = new int[col];
    if (matrix[i] == NULL)
    {
        cout << "Allocation memory - Failed";
        return;
    }
}

What can I do? Thank you,

解决方案

Simply, such an operator does not exist, so you can not overload it.

A possible solution is to define two classes: the Matrix and the Row.
You can define the operator[] of a Matrix so that it returns a Row, then define the same operator for the Row so that it returns an actual value (int or whatever you want, your Matrix could be also a template).
This way, the statement myMatrix[row][col] will be legal and meaningful.

The same can be done in order to assign a new Row to a Matrix or to change a value in a Row.

* EDIT *

As suggested in the comments, also you should take in consideration to use operator() instead of operator[] for such a case.
This way, there wouldn't be anymore the need for a Row class too.

这篇关于运算符重载 [][] 二维数组 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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