c++ Copy constructors and destructors

别说谁变了你拦得住时间么 提交于 2019-12-13 11:07:25

问题


I am learning constructors and Destructors in c++; Help me grasp my mistakes even if they are silly...

HERE is a code I have written to perform addition using classes in c++; This creates two summands of datatype num and employs the constructor sum() to perform sum of the two numbers; However when everything was goin' alright, I stumbled upon creating a copy constructor for num , (Although not necessary but still for practice)... without the dynamic object of the class sum it is not possible to run the code anyway(without removing the copy constructor)... Help me improve my code and my mistakes in the code below; Also I wanna know how to make use of the copy constructor in this program; the problem being that in the destructor the delete operation is being performed multiple times on the same piece of memory (I suppose)

Here's my Code

#include<iostream>
#include<new>
using namespace std;
class num
{
public:
    int *a;
    num(int x)
    {
        try
        {
            a=new int;
        }
        catch(bad_alloc xa)
        {
            cout<<"1";
            exit(1);
        }
        *a=x;
    }
    num(){  }
    num(const num &ob)
    {
        try
        {
            a=new int;
        }
        catch(bad_alloc xa)
        {
            cout<<"1''";
            exit(2);
        }
        *a=*(ob.a);
    }
    ~num()
    { 
        cout<<"Destruct!!!";
        delete a;
    }
};


class sum:public num
{
 public:
     int add;
     sum(num n1,num n2)
     {
         add=*(n1.a)+*(n2.a);
     }
     int getsum()
     {
         return add;
     }
};

int main()
{
    num x=58;
    num y=82;
    sum *s=new sum(x,y);
    cout<<s->getsum();
    delete s;
    return 0;
}

回答1:


I may miss something - didn't use new/delete for too long, but tried to correct all what I noticed.

P.S. always use smart pointers.

#include <iostream>
#include <exception>
#include <new>

using namespace std;

int* allocate(const char* err_msg, int exit_code)
{
    int* a = nullptr;
    try
    {
        a = new int;
    }
    catch (bad_alloc&)
    {
        cout << err_msg << endl;
        exit(exit_code);
    }
    return a;
}

class num
{
    int* a = nullptr; // always should be initialized here

public:
    num() noexcept : a(nullptr) // or here
    {}

    /*explicit*/ num(int x) : a(allocate("1", 1))
    {
        *a = x;
    }

    num(const num& ob) : a(allocate("1''", 2))
    {
        *a = *(ob.a);
    }

    // rule of zero/three/five
    // default copy assignment will copy pointer and one int will be leaked and one will be deleted twice
    num& operator =(const num& ob)
    {
        if (&ob == this)
        {
            return *this;
        }

        *a = *(ob.a);
        return *this;
    }

    ~num()
    { 
        cout << "Destruct!!!";
        delete a;
        a = nullptr; // usefull for debug
    }

    int value() const
    {
        if (a == nullptr)
        {
            throw runtime_error("a == nullptr");
        }
        return *a;
    }
};

class sum
{
    int add = 0;

public:
    sum(const num& n1, const num& n2)
    {
        add = n1.value() + n2.value();
    }

    int getsum() const
    {
        return add;
    }
};

int main()
{
    const num x = 58;
    const num y = 82;
    const sum* s = new sum(x, y);
    cout << s->getsum() << endl;
    delete s;
    return 0;
}


来源:https://stackoverflow.com/questions/51186060/c-copy-constructors-and-destructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!