C++ 调整大小和运算符=重载
我已经实现了一个名为LipidType的类,但是当调用resize函数和赋值操作符重载时,程序崩溃了,但是我在代码中找不到错误。这些是班上的私人成员C++ 调整大小和运算符=重载,c++,pointers,resize,variable-assignment,operator-keyword,C++,Pointers,Resize,Variable Assignment,Operator Keyword,我已经实现了一个名为LipidType的类,但是当调用resize函数和赋值操作符重载时,程序崩溃了,但是我在代码中找不到错误。这些是班上的私人成员 private: std::string *mod; std::string *classe; int matches; double mz; double *intensity; double *frag; std::string name; int maxsize; int
private:
std::string *mod;
std::string *classe;
int matches;
double mz;
double *intensity;
double *frag;
std::string name;
int maxsize;
int length;
这些就是功能
LipidType& LipidType::operator=(const LipidType& otherlip) //assignment operator
{
if (this == &otherlip) return *this; // handle self assignment
if(otherlip.maxsize > maxsize)
{
std::cout << "Resizing" << std::endl;
resize(otherlip.maxsize);
}
for(int i=0; i<otherlip.length; i++)
{
mod[i]=otherlip.mod[i];
classe[i]=otherlip.classe[i];
intensity[i]=otherlip.intensity[i];
frag[i]=otherlip.frag[i];
}
matches=otherlip.matches;
name=otherlip.name;
mz=otherlip.mz;
length = otherlip.length;
return *this;
}
void LipidType::resize(int nusize)
{
maxsize = nusize;
std::string* temp1=new std::string[maxsize];
std::string* temp2=new std::string[maxsize];
double* temp3=new double[maxsize];
double* temp4=new double[maxsize];
for(int i=0; i<length; i++)
{
temp1[i]=mod[i];
temp2[i]=classe[i];
temp3[i]=intensity[i];
temp4[i]=frag[i];
}
delete [] mod;
delete [] classe;
delete [] intensity;
delete [] frag;
mod=temp1;
classe=temp2;
intensity=temp3;
frag=temp4;
}
LipidType&LipidType::operator=(const LipidType&otherlip)//赋值运算符
{
如果(this==&otherlip)返回*this;//处理自分配
如果(otherlip.maxsize>maxsize)
{
std::cout我敢打赌这些指针是不必要的。如何将项目添加到源(otherlip
)对象?我的意思是,maxsize
是否可以降低西方的length
?我通过增加长度(length++)的函数添加项目(指针)如果长度+1>Max Stand调用了我可以容纳的大小调整函数,也可以考虑另一种类型,它包含2个STATES和2个双倍作为元素,所以你只有一个容器来考虑哪种类型只能在一个容器中保存字符串和加倍?