如何重载运算符 [] 让动态矩阵返回值!?C++
How to overload operator [] for dynamic matrix to return a value!? C++
class matrix {
public:
int **A;
int nrColoane;
int nrLinii;
public:
int& operator[] (int nrLinii, int nrColoane);
};
恐
怕这是不可能的。解决问题的常用方法是重载运算符 ()。
class Matrix {
public:
int& operator () (int i, int j);
};
正如其他人所提到的,使用 operator()
解决问题是很常见的。另一种方法是仍然使用 operator[]
接受一个参数,但返回一个表示行的对象(并保留对原始对象的引用)。然后行对象将再次实现operator[]
并返回实际值。
相关文章:
- 从python中调用C++函数并获取返回值
- 为什么模板类中的对象不能返回值
- 返回值优化:显式移动还是隐式
- lock_guard是否保护返回值
- 调用CreateProcess()并获取字符串的返回值
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 查找 GCD:并非所有控制路径都返回值
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 如何从 std::thread 返回值
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 方法错误"not all control paths return a value"和方法不返回值
- 动态数组传递给 main 中用随机值填充的函数,返回值3221226356
- 动态指针引用数组由三元运算符返回值,但有异常
- 是否可以将动态强制转换"this"作为返回值?
- 如何重载运算符 [] 让动态矩阵返回值!?C++
- 从方法指针动态创建泛型函数指针,推导返回值和参数
- 动态多态内存容器-返回值不正确
- c++函数中动态分配大结构时的返回值选择