我正在学习C++,目前我正在学习类的继承和为对象分配动态内存。我正在做一些练习,但现在我的应用程序遇到了一些奇怪的崩溃问题。
问题在于将派生类赋值给新对象。一切看起来都很正常,直到析构函数开始工作,我分析了一切,但是找不到错误。代码应该很简单,cd类存储简单的数据,“classic”类添加了一个字段。
(主要问题是应用程序在结束时崩溃)
这是源代码,我希望有人能帮助我。
问题在于将派生类赋值给新对象。一切看起来都很正常,直到析构函数开始工作,我分析了一切,但是找不到错误。代码应该很简单,cd类存储简单的数据,“classic”类添加了一个字段。
(主要问题是应用程序在结束时崩溃)
这是源代码,我希望有人能帮助我。
#ifndef CLASSIC_H_
#define CLASSIC_H_
class Cd
{
private:
char* performers;
char* label;
int selection;
double playtime;
public:
Cd(char* s1, char* s2, int n, double x);
Cd(const Cd& d);
Cd();
virtual ~Cd();
virtual void Report() const;
Cd& operator=(const Cd& d);
};
class Classic : public Cd
{
private:
char* maintrack;
public:
Classic(char* mt, char* s1, char* s2, int n, double x);
Classic();
Classic(const Classic& c);
Classic(char* mt, const Cd& d);
virtual void Report() const;
virtual ~Classic();
Classic& operator=(const Classic& c);
};
#endif
using std::strcpy;
Cd::Cd(char* s1, char* s2, int n, double x)
{
performers = new char[strlen(s1) + 1];
strcpy(performers, s1);
label = new char[strlen(s2) + 1];
strcpy(label, s2);
selection = n;
playtime = x;
}
Cd::Cd(const Cd& d)
{
performers = new char[strlen(d.performers) + 1];
strcpy(performers, d.performers);
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
selection = d.selection;
playtime = d.playtime;
}
Cd::~Cd()
{
delete [] performers;
delete [] label;
}
Cd::Cd()
{
performers = new char[1];
performers[0] = '\0';
label = new char[1];
label[0] = '\0';
selection = 0;
playtime = 0;
}
Cd& Cd::operator=(const Cd& d)
{
if (this == &d)
return *this;
delete [] performers;
delete [] label;
performers = new char[strlen(d.performers) + 1];
strcpy(performers, d.performers);
label = new char[strlen(d.label) + 1];
strcpy(label, d.label);
selection = d.selection;
playtime = d.playtime;
return *this;
}
void Cd::Report() const
{
using namespace std;
cout << performers << endl;
cout << label << endl;
cout << selection << endl;
cout << playtime << endl;
}
Classic::Classic(char* mt, char* s1, char* s2, int n, double x)
: Cd(s1, s2, n, x)
{
maintrack = new char[strlen(mt) + 1];
strcpy(maintrack, mt);
}
Classic::Classic() : Cd()
{
maintrack = new char[1];
maintrack[0] = '\0';
}
Classic::Classic(const Classic& c) : Cd(c)
{
maintrack = new char[strlen(c.maintrack) + 1];
strcpy(maintrack, c.maintrack);
}
Classic::Classic(char* mt, const Cd& d) : Cd(d)
{
maintrack = new char[strlen(mt) + 1];
strcpy(maintrack, mt);
}
void Classic::Report() const
{
Cd::Report();
std::cout << maintrack << std::endl;
}
Classic::~Classic()
{
delete [] maintrack;
}
Classic& Classic::operator=(const Classic& c)
{
if (this == &c)
return *this;
Cd::operator=(c);
delete [] maintrack;
maintrack = new char[strlen(c.maintrack)];
strcpy(maintrack, c.maintrack);
return *this;
}
#include <iostream>
#include "classic.h"
#include <cstdlib>
using namespace std;
void Bravo(const Cd& disk);
int main()
{
Cd c1("Beatles", "Capitol", 14, 35.5);
Classic c2 = Classic("Sonata fortepianowa B-dur, Fantazja C-moll",
"Alfred Brendel", "Philips", 2, 57.17);
Classic copy;
copy = c2;
copy.Report();
system("pause");
return 0;
}
void Bravo(const Cd& disk)
{
disk.Report();
}
std::string
而不是C风格的char*
字符串,那么您的问题将会迎刃而解。 - Mark Bmaintrack = new char[strlen(c.maintrack)];
这句话应该改为maintrack = new char[strlen(c.maintrack) + 1];
,对吗? - Matthew