双重免费或腐败(快速顶部) - c ++

时间:2018-02-03 02:15:37

标签: c++ class pointers error-handling destructor

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



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

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

&#13;
&#13;
#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;
}
&#13;
&#13;
&#13;

这是一个非常简单的主要代码,但我得到了双重免费或腐败(快速顶部)&#34;运行时错误。

我注意到当我删除Dase类中的析构函数时,这个问题就消失了。如果我想保留Dase的析构函数,我该怎么做才能解决这个问题?我要改变它的实施吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

您不应该明确调用析构函数。它是自动完成的。

所以,替换

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

Dase::~Dase()
{
}
相关问题