如果没有重载,数组索引运算符对 object 做了什么?
What does array index operator do on an object if not overloaded?
我正在编写 Matrix class 并且到了我需要创建 Array object 数组的地步,但是 Array 不能有不带参数的构造函数(它需要 m为它分配内存的参数)。我进行了搜索但没有找到解决方案,人们只建议使用 Vector object 来解决这个问题,但不允许我使用数组以外的任何其他方法来解决这个问题。然后一个朋友给我发了一个适合他们的矩阵的构造函数,但即使他们也不知道它为什么有效。
Matrix::Matrix(int n, int m):n(n),m(m) {
srand(time(0));
this->data=new Array(n);
for(int i=0; i<n; i++){
this->data[i].m=this->m;
this->data[i].data=new int[m];
for(int j=0; j<this->m; j++){
this->data[i].data[j]= rand()%10;
}
}
}
我不明白 this->data[i]
是如何定义的? [] 运算符不会在任何地方重载,数据只是一个数组 object 而不是 Array-s 的数组。那么为什么以及如何工作而不是编译错误?
源文件:
Header:
#define ROK04_ROK04_H
class Array{
protected:
int* data;
int m;
public:
Array(int m);
Array& operator = (const Array& a);
virtual ~Array();
Array& operator+=(const Array& a);
virtual void setElem(int pos, int value);
virtual void print() const;
friend class Matrix;
};
class Matrix{
protected:
Array* data;
int n;
int m;
public:
Matrix(int n, int m);
};
#endif //ROK04_ROK04_H ```
#ifndef ROK04_ROK04_H
#define ROK04_ROK04_H
class Array{
protected:
int* data;
int m;
public:
Array(int m);
Array& operator = (const Array& a);
virtual ~Array();
Array& operator+=(const Array& a);
virtual void setElem(int pos, int value);
virtual void stampa() const;
friend class Matrix;
};
class Matrix{
protected:
Array* data;
int n;
int m;
public:
Matrix(int n, int m);
void stampaj();
};
#endif //ROK04_ROK04_H
Cpp:
#include <iostream>
#include <ctime>
#include "rok04.h"
using namespace std;
Array::Array(int m):m(m),data( new int[m]() ){}
Array& Array::operator=(const Array& a) {
cout << "Usao" << this << endl;
for(int i = 0; i < m; i++){
data[i] = a.data[i];
}
return *this;
}
Array::~Array() {
delete[] data;
}
void Array::setElem(int pos, int value) {
try {
if(pos >= m){
cout << "Usao\n";
throw out_of_range("Index out of bounds!");
}
data[pos] = value;
} catch (out_of_range& oor){
cerr << oor.what() << endl;
}
}
void Array::print() const {
// cout << m;
for (int i = 0; i < m; ++i) {
cout << data[i] << " ";
}
cout << endl;
}
Array &Array::operator+=(const Array& a) {
int* temp = new int(m+a.m);
for(int i = 0; i < m; i++){
temp[i] = data[i];
}
for (int i = m; i < m+a.m; ++i) {
temp[i] = a.data[i-m];
}
delete[] data;
data = temp;
m = m+a.m;
return *this;
}
Matrix::Matrix(int n, int m):n(n),m(m) {
srand(time(0));
this->data=new Array(n);
for(int i=0; i<n; i++){
this->data[i].m=this->m;
this->data[i].data=new int[m];
for(int j=0; j<this->m; j++){
this->data[i].data[j]= rand()%10;
}
}
}
你有这个定义:int* data
。所以 by default,给定一个指针操作数,data[x]
被 C++ 编译器翻译成 *(data + x)
。
我正在编写 Matrix class 并且到了我需要创建 Array object 数组的地步,但是 Array 不能有不带参数的构造函数(它需要 m为它分配内存的参数)。我进行了搜索但没有找到解决方案,人们只建议使用 Vector object 来解决这个问题,但不允许我使用数组以外的任何其他方法来解决这个问题。然后一个朋友给我发了一个适合他们的矩阵的构造函数,但即使他们也不知道它为什么有效。
Matrix::Matrix(int n, int m):n(n),m(m) {
srand(time(0));
this->data=new Array(n);
for(int i=0; i<n; i++){
this->data[i].m=this->m;
this->data[i].data=new int[m];
for(int j=0; j<this->m; j++){
this->data[i].data[j]= rand()%10;
}
}
}
我不明白 this->data[i]
是如何定义的? [] 运算符不会在任何地方重载,数据只是一个数组 object 而不是 Array-s 的数组。那么为什么以及如何工作而不是编译错误?
源文件:
Header:
#define ROK04_ROK04_H
class Array{
protected:
int* data;
int m;
public:
Array(int m);
Array& operator = (const Array& a);
virtual ~Array();
Array& operator+=(const Array& a);
virtual void setElem(int pos, int value);
virtual void print() const;
friend class Matrix;
};
class Matrix{
protected:
Array* data;
int n;
int m;
public:
Matrix(int n, int m);
};
#endif //ROK04_ROK04_H ```
#ifndef ROK04_ROK04_H
#define ROK04_ROK04_H
class Array{
protected:
int* data;
int m;
public:
Array(int m);
Array& operator = (const Array& a);
virtual ~Array();
Array& operator+=(const Array& a);
virtual void setElem(int pos, int value);
virtual void stampa() const;
friend class Matrix;
};
class Matrix{
protected:
Array* data;
int n;
int m;
public:
Matrix(int n, int m);
void stampaj();
};
#endif //ROK04_ROK04_H
Cpp:
#include <iostream>
#include <ctime>
#include "rok04.h"
using namespace std;
Array::Array(int m):m(m),data( new int[m]() ){}
Array& Array::operator=(const Array& a) {
cout << "Usao" << this << endl;
for(int i = 0; i < m; i++){
data[i] = a.data[i];
}
return *this;
}
Array::~Array() {
delete[] data;
}
void Array::setElem(int pos, int value) {
try {
if(pos >= m){
cout << "Usao\n";
throw out_of_range("Index out of bounds!");
}
data[pos] = value;
} catch (out_of_range& oor){
cerr << oor.what() << endl;
}
}
void Array::print() const {
// cout << m;
for (int i = 0; i < m; ++i) {
cout << data[i] << " ";
}
cout << endl;
}
Array &Array::operator+=(const Array& a) {
int* temp = new int(m+a.m);
for(int i = 0; i < m; i++){
temp[i] = data[i];
}
for (int i = m; i < m+a.m; ++i) {
temp[i] = a.data[i-m];
}
delete[] data;
data = temp;
m = m+a.m;
return *this;
}
Matrix::Matrix(int n, int m):n(n),m(m) {
srand(time(0));
this->data=new Array(n);
for(int i=0; i<n; i++){
this->data[i].m=this->m;
this->data[i].data=new int[m];
for(int j=0; j<this->m; j++){
this->data[i].data[j]= rand()%10;
}
}
}
你有这个定义:int* data
。所以 by default,给定一个指针操作数,data[x]
被 C++ 编译器翻译成 *(data + x)
。