64,646
社区成员
发帖
与我相关
我的任务
分享
template<class T>
class Matrix
{
public:
unsigned rows;
unsigned cols;
T **p;
public:
Matrix():rows(0),cols(0){p=NULL;}
Matrix(unsigned r,unsigned c);
~Matrix();
Matrix<T> Reverse(); //矩阵转置
T* operator [](unsigned i); //矩阵的下标访问
Matrix<T> operator +(const Matrix<T> &a); //矩阵相加
Matrix<T> operator *(const Matrix<T> &a); //矩阵相乘
Matrix<T> operator =(const Matrix<T> &a); //矩阵赋值
template <class Type > friend ostream& operator <<(ostream & os,const Matrix<Type> & a); //重载输出操作符
};
//析构函数
template <class T>
Matrix<T>::~Matrix()
{
if(NULL!=p)
{
for(int i=0;i<rows;i++)
delete[] p[i];
delete p;
}
}
#include"matrix.h"
#include<iostream>
using namespace std;
int main()
{
Matrix<int> matrix3(5,5);
Matrix<int> matrix4=matrix3;//此处调用合成的拷贝构造函数
return 0;//主函数结束时,matrix3和matrix4都会调用析构函数,它们的指针成员p指向的应该是同一块内存,为什么释放两次不会出错。在vs08和dev c++上都能正确执行
}
char* p = new char[128];
delete []p;
delete []p;