C++ 如何创建双 class 运算符 [][]
C++ how to create double class operator [][]
我正在构建一个能够管理矩阵的类型,所以我搜索了如何制作 [][] 运算符,但运气不好,所以不知道该怎么做,我只需要一种制作双运算符的方法
这是 class 正在建设
#include<iostream>
#include<conio.h>
using namespace std;
class ddouble{
private:
unsigned short int x, y;
public:
ddouble();
ddouble(unsigned short int, unsigned short int);
double **M;
void read();
void print();
};
ddouble::ddouble(unsigned short int m, unsigned short int n){
for (int i = 0; i < m; i++){
M = new (nothrow) double *[i];
for (int I = 0; I < n; I++){
M[i] = new (nothrow) double[I];
}
}
}
void ddouble::read(){
for (int i = 0; i < x; i++){
cout << "plz enter line \n";
for (int I = 0; I < y; I++){
cin >> M[i][I];
}
}
}
void ddouble::print(){
cout << "i,j\t|\t";
for (int i = 0; i < y; i++){
cout << i << "\t";
}
cout << endl;
for (int i = 0; i < x; i++){
cout << i << "\t|\t";
for (int I = 0; I < y; I++){
cout << M[i][I] << "\t";
}
cout << endl;
}
}
void main(){
ddouble a(2, 2);
a.read();
a.print();
_getch();
}
您可以实施代理 class 来提供该行为。粗略的草图是这样的:-
template<class T>
class Array2D
{
public:
class Array1D
{
public:
T& operator[](int index);
const T& operator[](int index) const;
...
};
Array1D operator[](int index);
const Array1D operator[](int index) const;
...
};
此 class 的客户端不知道内部代理对象。我建议您选择 std::vector<std::vector<double> >
。但是在某些情况下,您希望汇总自己的 class 来为客户要求提供特定的行为,例如绑定检查等...
我建议使用 std::vector<std::vector<double>>
。也就是说,鉴于您现在拥有的结构,您可以简单地定义一个 operator[]
那个 returns 一个 double *
,例如:
double * ddouble::operator[](int i) {
return M[i];
}
您现在可以在 main
中访问变量 a[i][j]
。
此外,您还缺少一个释放内存的析构函数;没有它你有内存泄漏。您应该将析构函数定义为:
void ddouble::~ddouble() {
for(int I = 0; I < y; I++) {
delete [] M[I];
}
delete [] M;
}
就我个人而言,我不会。我会选择简单的选项,并提供一个带有两个参数的函数(或可能的 operator()
)。
如果你真的想支持 [][]
语法,那么你需要首先调用 return 一些代理类型,它也会重载 operator[]
以给出最终结果。这将遵循
class proxy {
double * p;
public:
proxy(double * p) : p(p) {}
double & operator[](size_t i) {return p[i];}
};
proxy operator[](size_t i) {return proxy(M[i]);}
为 const 正确性添加必要的重载留作练习。
在这种简单的情况下,代理可以只是一个 double*
指针;如果访问更复杂,或者如果您想添加边界检查或其他访问控制,则需要 class。
(和往常一样,当您编写资源管理 class 时,不要忘记 Rule of Three)。
我正在构建一个能够管理矩阵的类型,所以我搜索了如何制作 [][] 运算符,但运气不好,所以不知道该怎么做,我只需要一种制作双运算符的方法 这是 class 正在建设
#include<iostream>
#include<conio.h>
using namespace std;
class ddouble{
private:
unsigned short int x, y;
public:
ddouble();
ddouble(unsigned short int, unsigned short int);
double **M;
void read();
void print();
};
ddouble::ddouble(unsigned short int m, unsigned short int n){
for (int i = 0; i < m; i++){
M = new (nothrow) double *[i];
for (int I = 0; I < n; I++){
M[i] = new (nothrow) double[I];
}
}
}
void ddouble::read(){
for (int i = 0; i < x; i++){
cout << "plz enter line \n";
for (int I = 0; I < y; I++){
cin >> M[i][I];
}
}
}
void ddouble::print(){
cout << "i,j\t|\t";
for (int i = 0; i < y; i++){
cout << i << "\t";
}
cout << endl;
for (int i = 0; i < x; i++){
cout << i << "\t|\t";
for (int I = 0; I < y; I++){
cout << M[i][I] << "\t";
}
cout << endl;
}
}
void main(){
ddouble a(2, 2);
a.read();
a.print();
_getch();
}
您可以实施代理 class 来提供该行为。粗略的草图是这样的:-
template<class T>
class Array2D
{
public:
class Array1D
{
public:
T& operator[](int index);
const T& operator[](int index) const;
...
};
Array1D operator[](int index);
const Array1D operator[](int index) const;
...
};
此 class 的客户端不知道内部代理对象。我建议您选择 std::vector<std::vector<double> >
。但是在某些情况下,您希望汇总自己的 class 来为客户要求提供特定的行为,例如绑定检查等...
我建议使用 std::vector<std::vector<double>>
。也就是说,鉴于您现在拥有的结构,您可以简单地定义一个 operator[]
那个 returns 一个 double *
,例如:
double * ddouble::operator[](int i) {
return M[i];
}
您现在可以在 main
中访问变量 a[i][j]
。
此外,您还缺少一个释放内存的析构函数;没有它你有内存泄漏。您应该将析构函数定义为:
void ddouble::~ddouble() {
for(int I = 0; I < y; I++) {
delete [] M[I];
}
delete [] M;
}
就我个人而言,我不会。我会选择简单的选项,并提供一个带有两个参数的函数(或可能的 operator()
)。
如果你真的想支持 [][]
语法,那么你需要首先调用 return 一些代理类型,它也会重载 operator[]
以给出最终结果。这将遵循
class proxy {
double * p;
public:
proxy(double * p) : p(p) {}
double & operator[](size_t i) {return p[i];}
};
proxy operator[](size_t i) {return proxy(M[i]);}
为 const 正确性添加必要的重载留作练习。
在这种简单的情况下,代理可以只是一个 double*
指针;如果访问更复杂,或者如果您想添加边界检查或其他访问控制,则需要 class。
(和往常一样,当您编写资源管理 class 时,不要忘记 Rule of Three)。