C ++重载运算符[] []

编程入门 行业动态 更新时间:2024-10-27 18:33:35
本文介绍了C ++重载运算符[] []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有类CMatrix,其中是值的数组的双指针。

I have class CMatrix, where is "double pointer" to array of values.

class CMatrix { public: int rows, cols; int **arr; };

我只需要键入以下内容来访问矩阵的值:

I simply need to access the values of matrix by typing:

CMatrix x; x[0][0] = 23;

我知道如何使用:

x(0,0) = 23;

但我真的需要这样做。任何人可以帮助我吗?请?

But I really need to do that the other way. Can anyone help me with that? Please?

感谢大家的帮助,我这样做了...

Thank you guys for help at the end I did it this way...

class CMatrix { public: int rows, cols; int **arr; public: int const* operator[]( int const y ) const { return &arr[0][y]; } int* operator[]( int const y ) { return &arr[0][y]; } ....

帮助我真的很感激它!

推荐答案

没有 operator [] [] 在C ++中。但是,您可以重载 operator [] 以返回另一个结构,并且在重载 operator [] 效果。

There is no operator[][] in C++. However, you can overload operator[] to return another structure, and in that overload operator[] too to get the effect you want.

更多推荐

C ++重载运算符[] []

本文发布于:2023-06-03 12:14:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!