C++基础问题(*this,及const引用)
最近在做一个矩阵的类,
class CXMref;
class CXMatrix
{
public:
friend class CMO ;
// construction and destruction
CXMatrix() ; // default constructor
CXMatrix(double val);
CXMatrix(int nRowsH, int nCols,double* pData) ; // constructs an empty matrix of this size
CXMatrix(const CXMatrix &other) ; // copy constructor
CXMatrix(int nRowsH, int nCols =1) ; // constructs an empty matrix of this size
Null();
virtual ~CXMatrix(); // destructor
CXMref GetElement(int nRowsH, int nCol) const;
CXMref GetElement(int nPos) const;
bool shared () { return (m_pMat->GetRef() > 1); }
inline int GetNumColumns() const { return m_NumColumns ; } ;
inline int GetNumRows() const { return m_NumRowsH ; } ;
inline int size() const { return m_NumColumns *m_NumRowsH; } ;
void clone ();
CXMatrix operator +(const CXMatrix& mat2) const;
CXMatrix operator -(const CXMatrix& mat2) const;
CXMatrix operator *(const CXMatrix& mat2) const;
CXMatrix operator /(const CXMatrix& mat2) const;
CXMatrix operator +(const double& mat2) const;
CXMatrix operator -(const double& mat2) const;
CXMatrix operator *(const double& mat2) const;
CXMatrix operator /(const double& mat2) const;
CXMatrix operator !() const;
CXMatrix operator ~() const;
CXMatrix operator -() const;
CXMatrix& operator = (const CXMatrix &other) ;
CXMatrix& operator += (const CXMatrix& mat2) ;
CXMatrix& operator -= (const CXMatrix& mat2) ;
CXMatrix& operator *= (const CXMatrix &other) ;
CXMatrix& operator /= (const CXMatrix& mat2) ;
CXMatrix& operator = (const double &other) ;
CXMref operator ()(UINT x,UINT y);
CXMref operator ()(UINT x);
MATTYPE GetMatType()const;
bool isSquare () const { return (m_NumColumns == m_NumRowsH); }
private:
protected:
// internal variables
int m_NumColumns ; // number of columns in matrix
int m_NumRowsH ; // number of rows in matrix
double* m_pData ; // pointer to data, may be shared among objects
baseMatrix* m_pMat;
friend class CXMref;
};
其中的CXMref是为了给出矩阵元素的引用#include "XMatrix.h"
class CXMref
{
typedef double T;
public:
CXMref( CXMatrix& mm, size_t ii): m(mm), i(ii) {;}
CXMref( CXMref& mr): m(mr.m), i(mr.i) {;};
operator double() const{return m.m_pData[i]; }
double* operator& () const{return &m.m_pData[i]; }
double operator+ (double mux) const{return m.m_pData[i] + mux; }
double operator- (double mux) const{return m.m_pData[i] - mux; }
double operator* (double mux) const{return m.m_pData[i] * mux; }
double operator/ (double mux) const{return m.m_pData[i] / mux; }
double& operator = (const double& e) const{ if (m.shared()) m.clone(); return m.m_pData[i]= e; }
double& operator+= (double mux) const{if (m.shared()) m.clone();return m.m_pData[i] += mux; }
double& operator-= (double mux) const{if (m.shared()) m.clone();return m.m_pData[i] -= mux; }
double& operator*= (double mux) const{if (m.shared()) m.clone();return m.m_pData[i] *= mux; }
double& operator/= (double mux) const{if (m.shared()) m.clone();return m.m_pData[i] /= mux; }
ine ostream&operator<< (ostream& os){const double x = m.m_pData[i];os << x;return os;}
virtual ~CXMref();
private:
CXMref ();
CXMatrix& m;
size_t i;
};
能通过CXMref给出矩阵元素引用的方法有:
GetElement及()运算符
如:
Matrix X = CMO::zeros(1,7);
X (5)= 4;//X.GetElement(5);
首先是第一个问题:
在GetElement函数中,我需要构造一个CXMref 并返回它,但是我为什么需要强制转换一下呢?
CXMref CXMatrix::GetElement(int nPos) const
{
return CXMref ((CXMatrix&)*this,nPos);//为什么这里不能写成 return CXMref ((CXMatrix&)*this,nPos);
//如果按照后面的写法会得到这样的编译错误:error C2665: 'CXMref::CXMref' : none of the 3 overloads can convert parameter 1 from type 'const class CXMatrix'
}
这是第一个疑问。这个疑问虽然能够通过强制类型转换解决,但却使我比较迷惑。
第二个问题是,我在另外一个类CMO里面定义了一些静态的成员函数,对矩阵进行操作,包括了上面的CMO::zeros(1,7)(用来生成一个元素都是0的矩阵);
在这个类里面有这样一个函数:
void CMO::Display(const Matrix & mat)
{
for (int i = 0;i<mat.MatRowNo;i++)
{
for (int j = 0;j<mat.MatColNo;j++)
{
cout<<mat.GetElement(i,j)<<" ";//这里为什么不能写成:cout<<mat(i,j)<<" ";
//如果写成后面的,那么会得到这样的编译错误:error C2663: '()' : 2 overloads have no legal conversion for 'this' pointer
//其实GetElement和()运算符里面的代码是完全一样的,为什么后者就不能运行?
}
cout<<endl;
}
cout<<endl;
}
而如果把上面的函数定义成下面的形式它又是能够通过的:
void CMO::Display(Matrix & mat)
{
for (int i = 0;i<mat.MatRowNo;i++)
{
for (int j = 0;j<mat.MatColNo;j++)
{
cout<<mat(i,j)<<" ";
}
cout<<endl;
}
cout<<endl;
}
这种声明与前面的声明仅少了一个const,最后却有这样的差别,平时对C++的基础也看了不少,可是实在弄不清楚上面的两个问题究竟是怎么回事儿啊,请大虾们出手相助,谢谢!
注:所用编译器为VC6。