我刚遇到一个奇怪的问题。我将在下面粘贴完整的代码:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

    // declare a base class
    class Base
    {
      public:
        Base();
        Base(int n, int arr[2]);
        Base(const Base &base);

        Base &operator=(const Base &rhs);

        int getSize() const;
        int *getA() const;

        ~Base();

      private:
        int *a;
        int size;
    };

    class Case
    {
      public:
        Case();
        Case(int n, int arr[2]);
        Case(const Case &rhs);

        Case &operator=(const Case &rhs);

        int getSize() const;
        int *getA() const;

        ~Case();

      private:
        int *a;
        int size;
    };

    class Dase
    {
      public:
        Dase();
        Dase(int bSize, int barr[2], int cSize, int carr[2]);
        Dase(const Dase &dase);

        Dase &operator=(const Dase &rhs);

        Base getB() const;
        Case getC() const;

        ~Dase();

      private:
        Base b;
        Case c;
    };

// implementation
Base::Base() : size(0)
{
    a = NULL;
}

Base::Base(int n, int arr[2]) : size(n)
{
    a = new int[n];
    for (int i = 0; i < size; i++)
        a[i] = arr[i];
}

Base::Base(const Base &base)
{
    size = base.getSize();

    a = new int[size];
    for (int i = 0; i < size; i++)
        a[i] = base.getA()[i];
}

Base &Base::operator=(const Base &rhs)
{
    if (this == &rhs)
        return *this;

    size = rhs.getSize();
    delete[] a;
    a = new int[size];
    for (int i = 0; i < size; i++)
        a[i] = rhs.getA()[i];
    return *this;
}

int *Base::getA() const
{
    return a;
}

int Base::getSize() const
{
    return size;
}

Base::~Base()
{
    delete[] a;
}

Case::Case() : size(0)
{
    a = NULL;
}

Case::Case(int n, int arr[2]) : size(n)
{
    a = new int[n];
    for (int i = 0; i < size; i++)
        a[i] = arr[i];
}

Case::Case(const Case &rhs)
{
    size = rhs.getSize();

    a = new int[size];
    for (int i = 0; i < size; i++)
        a[i] = rhs.getA()[i];
}

Case &Case::operator=(const Case &rhs)
{
    if (this == &rhs)
        return *this;

    size = rhs.getSize();
    delete[] a;
    a = new int[size];
    for (int i = 0; i < size; i++)
        a[i] = rhs.getA()[i];

    return *this;
}

int *Case::getA() const
{
    return a;
}

int Case::getSize() const
{
    return size;
}

Case::~Case()
{
    delete[] a;
}

// implement class Dase
Dase::Dase() : b(Base()), c(Case())
{
    // delebrately left empty
}

Dase::Dase(int bSize, int barr[2], int cSize, int carr[2])
{
    b = Base(bSize, barr);
    c = Case(cSize, carr);
}

Dase::Dase(const Dase &dase)
{
    b = dase.getB();
    c = dase.getC();
}

Dase &Dase::operator=(const Dase &rhs)
{
    if (this == &rhs)
        return *this;

    b = rhs.getB();
    c = rhs.getC();

    return *this;
}

Base Dase::getB() const
{
    return b;
}

Case Dase::getC() const
{
    return c;
}

Dase::~Dase()
{
    b.~Base();
    c.~Case();
}


在上面的代码中,我定义了3个类:Base,Case和Dase。我包括了他们的声明和实现。以下是主要代码:

#include "classes.h"

int main()
{
    int arr[2] = {1, 2};
    int brr[2] = {3, 4};

    Dase d1(2, arr, 2, brr);
    Dase d2;

    d2 = d1;
}


这是一个非常简单的主代码,但是在运行时出现“双重释放或损坏(快速停止)”错误。

我注意到,当我在Dase类中删除析构函数时,此问题就消失了。如果要保留Dase的析构函数,该怎么办才能解决此问题?我应该更改其实现吗?

谢谢!

最佳答案

您不应显式调用析构函数。它是自动完成的。

因此,更换

Dase::~Dase()
{
    b.~Base();
    c.~Case();
}


Dase::~Dase()
{
}

08-20 04:21