【发布时间】:2015-07-04 02:35:22
【问题描述】:
我在用动态数组重载 = 运算符时遇到了麻烦。这就是我到目前为止所拥有的。另外我知道我还没有编写析构函数或构造函数,但我需要先关注这个运算符:
在我的头文件中:
#ifndef fasdf_dynn_h
#define fasdf_dynn_h
#include <iostream>
#include <fstream>
#include<string>
#include <cstdlib>
#include <vector>
using namespace std;
template <class T>
class MatrixdynVector{
public:
template <class H>
MatrixdynVector<H>& operator =(const MatrixdynVector<H>& c)
{
if (this == &c)//checks for self assignment
{
return *this;
}
else
{
delete [] matrix;
matrix=new int[c.m*n];
this->m=c.m;
this->n=c.n;
return *this;
}
}
private:
int m,n;
int** matrix;
};
#endif
【问题讨论】:
-
您能详细说明您遇到了什么麻烦吗?
-
@user4578093 从字面上看,我不知道自己在做什么,我需要帮助来重载动态数组的 = 运算符。我不知道我是否在正确的轨道上我的记忆力很弱
-
即使你有一个“模板化操作符=”,你仍然需要非模板化版本,否则编译器会为你生成一个。
标签: c++ overloading assignment-operator rule-of-three