c++中运算符重载的问题,提供下思路
设计一个CMatrix类,以及重载[]操作符使得我们可以像访问数组一样访问CMatrix的元素例如CMatrixa;我们可以使用a[3][4]来访问其元素。只需提供思路即...
设计一个CMatrix类,以及重载[]操作符使得我们可以像访问数组一样访问CMatrix的元素例如CMatrix a;我们可以使用a[3][4]来访问其元素。
只需提供思路即可,问题是【】和+不是很一样,+的重载很简单的。
楼下的什么意思啊,糊弄人吗? 展开
只需提供思路即可,问题是【】和+不是很一样,+的重载很简单的。
楼下的什么意思啊,糊弄人吗? 展开
3个回答
展开全部
思路: 用一个矩阵类和一个辅助类完成
矩阵类中用double *指针存储顺序数据,用动态数组存储每个维的大小
矩阵类重载[] ,初始化一个辅助类对象, 填写当前维\当前内存偏移
//辅助类,返回对某一维的访问权
// Matirx.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
class CMatirxDim;
class CMatirx;
//辅助类,返回对某一维的访问权
class CMatrixDim{
public:
CMatirx * m_pMarix;
int nCurrDim;
double * m_pCurrOffset;
CMatrixDim operator [] (int n);
operator double () ;
CMatrixDim & operator = (double n)
{
*m_pCurrOffset = n;
return *this;
}
};
class CMatirx{
public:
double * m_pBuf; //内存
int m_nDim; //多少维
int m_nDimSize[16]; //每一维大小,最多16维
public:
CMatrixDim operator [] (int n)
{
CMatrixDim dim;
dim.m_pMarix = this;
dim.nCurrDim = 1;
dim.m_pCurrOffset = m_pBuf+m_nDimSize[0]*n;
return dim;
}
};
CMatrixDim CMatrixDim::operator [] (int n)
{
//在这里判断\返回CMatrixDim对象
CMatrixDim dim;
dim.m_pMarix = m_pMarix;
dim.nCurrDim = nCurrDim+1;
dim.m_pCurrOffset = m_pCurrOffset+n*m_pMarix->m_nDimSize[nCurrDim];
return dim;
}
CMatrixDim::operator double ()
{
return *m_pCurrOffset;
}
int _tmain(int argc, _TCHAR* argv[])
{
double p[2][3][4];
CMatirx matrix;
matrix.m_nDim = 3;
matrix.m_pBuf = (double *)p;
matrix.m_nDimSize[0] = 3*4;
matrix.m_nDimSize[1] = 4;
matrix.m_nDimSize[2] = 1;
matrix.m_nDimSize[3] = 0;
for (int i=0;i<2;i++)
{
for (int j=0;j<3;j++)
{
for (int k=0;k<4;k++)
matrix[i][j][k] = (double)(rand()%1000)/10.0;
}
}
for (int i=0;i<2;i++)
{
for (int j=0;j<3;j++)
{
for (int k=0;k<4;k++)
printf("matrix(%d,%d,%d)= %lf\n",
i,j,k,
(double)(matrix[i][j][k])
);
}
}
return 0;
}
用VC 2005/2008编译后运行:
matrix(0,0,0)= 4.100000
matrix(0,0,1)= 46.700000
matrix(0,0,2)= 33.400000
matrix(0,0,3)= 50.000000
matrix(0,1,0)= 16.900000
matrix(0,1,1)= 72.400000
matrix(0,1,2)= 47.800000
matrix(0,1,3)= 35.800000
matrix(0,2,0)= 96.200000
matrix(0,2,1)= 46.400000
matrix(0,2,2)= 70.500000
matrix(0,2,3)= 14.500000
matrix(1,0,0)= 28.100000
matrix(1,0,1)= 82.700000
matrix(1,0,2)= 96.100000
matrix(1,0,3)= 49.100000
matrix(1,1,0)= 99.500000
matrix(1,1,1)= 94.200000
matrix(1,1,2)= 82.700000
matrix(1,1,3)= 43.600000
matrix(1,2,0)= 39.100000
matrix(1,2,1)= 60.400000
matrix(1,2,2)= 90.200000
matrix(1,2,3)= 15.300000
请按任意键继续. . .
矩阵类中用double *指针存储顺序数据,用动态数组存储每个维的大小
矩阵类重载[] ,初始化一个辅助类对象, 填写当前维\当前内存偏移
//辅助类,返回对某一维的访问权
// Matirx.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
class CMatirxDim;
class CMatirx;
//辅助类,返回对某一维的访问权
class CMatrixDim{
public:
CMatirx * m_pMarix;
int nCurrDim;
double * m_pCurrOffset;
CMatrixDim operator [] (int n);
operator double () ;
CMatrixDim & operator = (double n)
{
*m_pCurrOffset = n;
return *this;
}
};
class CMatirx{
public:
double * m_pBuf; //内存
int m_nDim; //多少维
int m_nDimSize[16]; //每一维大小,最多16维
public:
CMatrixDim operator [] (int n)
{
CMatrixDim dim;
dim.m_pMarix = this;
dim.nCurrDim = 1;
dim.m_pCurrOffset = m_pBuf+m_nDimSize[0]*n;
return dim;
}
};
CMatrixDim CMatrixDim::operator [] (int n)
{
//在这里判断\返回CMatrixDim对象
CMatrixDim dim;
dim.m_pMarix = m_pMarix;
dim.nCurrDim = nCurrDim+1;
dim.m_pCurrOffset = m_pCurrOffset+n*m_pMarix->m_nDimSize[nCurrDim];
return dim;
}
CMatrixDim::operator double ()
{
return *m_pCurrOffset;
}
int _tmain(int argc, _TCHAR* argv[])
{
double p[2][3][4];
CMatirx matrix;
matrix.m_nDim = 3;
matrix.m_pBuf = (double *)p;
matrix.m_nDimSize[0] = 3*4;
matrix.m_nDimSize[1] = 4;
matrix.m_nDimSize[2] = 1;
matrix.m_nDimSize[3] = 0;
for (int i=0;i<2;i++)
{
for (int j=0;j<3;j++)
{
for (int k=0;k<4;k++)
matrix[i][j][k] = (double)(rand()%1000)/10.0;
}
}
for (int i=0;i<2;i++)
{
for (int j=0;j<3;j++)
{
for (int k=0;k<4;k++)
printf("matrix(%d,%d,%d)= %lf\n",
i,j,k,
(double)(matrix[i][j][k])
);
}
}
return 0;
}
用VC 2005/2008编译后运行:
matrix(0,0,0)= 4.100000
matrix(0,0,1)= 46.700000
matrix(0,0,2)= 33.400000
matrix(0,0,3)= 50.000000
matrix(0,1,0)= 16.900000
matrix(0,1,1)= 72.400000
matrix(0,1,2)= 47.800000
matrix(0,1,3)= 35.800000
matrix(0,2,0)= 96.200000
matrix(0,2,1)= 46.400000
matrix(0,2,2)= 70.500000
matrix(0,2,3)= 14.500000
matrix(1,0,0)= 28.100000
matrix(1,0,1)= 82.700000
matrix(1,0,2)= 96.100000
matrix(1,0,3)= 49.100000
matrix(1,1,0)= 99.500000
matrix(1,1,1)= 94.200000
matrix(1,1,2)= 82.700000
matrix(1,1,3)= 43.600000
matrix(1,2,0)= 39.100000
matrix(1,2,1)= 60.400000
matrix(1,2,2)= 90.200000
matrix(1,2,3)= 15.300000
请按任意键继续. . .
展开全部
#include <iostream>
using namespace std;
class Matrix {
private:
int a[3][4];
public:
int* operator [](int i) {
return a[i];
}
};
main() {
Matrix m;
m[0][0] = 5;
cout<<m[0][0]<<endl;
m[0][0] = 6;
cout<<m[0][0]<<endl;
}
using namespace std;
class Matrix {
private:
int a[3][4];
public:
int* operator [](int i) {
return a[i];
}
};
main() {
Matrix m;
m[0][0] = 5;
cout<<m[0][0]<<endl;
m[0][0] = 6;
cout<<m[0][0]<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
其实不用想的那么复杂
和输入输出操作符>><<是一样的道理
只要返回它的引用就可以了
CMatrix& CMatrix::operator[](int n)
{
...
return *this;
}
和输入输出操作符>><<是一样的道理
只要返回它的引用就可以了
CMatrix& CMatrix::operator[](int n)
{
...
return *this;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询