C++:继承,赋值

3
我正在学习C++,目前我正在学习类的继承和为对象分配动态内存。我正在做一些练习,但现在我的应用程序遇到了一些奇怪的崩溃问题。
问题在于将派生类赋值给新对象。一切看起来都很正常,直到析构函数开始工作,我分析了一切,但是找不到错误。代码应该很简单,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();
}

应用程序在崩溃时是否会显示错误消息?如果是,您也应该发布它。 - NominSim
在调试器中运行您的程序,您将看到崩溃发生的位置。 - interjay
@Chris .cpp代码在“#endif”下面。 - jrad
1
我怀疑如果您使用std::string而不是C风格的char*字符串,那么您的问题将会迎刃而解。 - Mark B
1
maintrack = new char[strlen(c.maintrack)]; 这句话应该改为 maintrack = new char[strlen(c.maintrack) + 1];,对吗? - Matthew
显示剩余7条评论
2个回答

4

问题出现在你的Classic复制构造函数中。然而,这也凸显了自己进行字符串操作的风险。我建议重新编写整个练习,不再使用char*,而是改用std::string

Classic& Classic::operator=(const Classic& c)
{
    if (this == &c)
        return *this;

    Cd::operator=(c);
    delete [] maintrack;

        // you need strlen(c.maintrack) + 1
    maintrack = new char[strlen(c.maintrack)];
    strcpy(maintrack, c.maintrack);

    return *this;
}

1
你是我的更快版本吗? - Wug
是的,我刚刚意识到了这一点。当然使用std::string会更简单,但这是为了训练所以使用char*和'new'。无论如何,非常感谢! - ashur

3
可能存在经典类Classic& Classic::operator=(const Classic& c)中的内存损坏问题:
maintrack = new char[strlen(c.maintrack)];
strcpy(maintrack, c.maintrack);

请将以下内容替换为:

maintrack = new char[strlen(c.maintrack) + 1];
strcpy(maintrack, c.maintrack);

话虽如此,为什么您不直接使用标准库中的字符串类呢?这样可以避免这种以及许多其他麻烦。它会自动处理所有的内存管理、空终止等问题。

string maintrack;
char * derp = "derp";
maintrack = derp; // valid
maintrack = "derp"; // valid
maintrack = another_string; // valid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接