Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

class - c++ Copy constructors and destructors

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...