3

我正在学习 C++,目前我正在继承类和对象的动态内存分配。我正在做一些练习,现在我的应用程序出现了一些奇怪的崩溃。

问题在于将派生类分配给新对象。一切看起来都很好,直到析构函数开始工作,我分析了一切,我找不到错误。代码应该很简单,类 cd 存储简单的数据,“经典”是添加一个字段。

(主要问题是应用程序最终崩溃)

这是源代码,希望有人能帮助我

#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();
}
4

2 回答 2

4

问题出在您的复制构造函数中Classic。然而,这只是突出了进行自己的字符串操作的危险。我建议改用 nochar*和 using重写整个练习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;
}
于 2012-07-19T14:22:35.380 回答
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
于 2012-07-19T14:24:55.553 回答