Search code examples
c++operator-overloadingdeque

operator overloading +=


I must write operator overloading += but I don't know how to do it correctly (I began to write it a the end of code it wasn't correct so i delete all except you see).

#include <iostream>
using namespace std;

class dek
{
private:
    int *x,na4,kon,n,razmer;
public:
    dek(int m)
    {
        x=new int[m];
        n=m;
        na4=1;
        kon=0;
        razmer=0;
    }
    ~dek()
    {
        delete [] x;
    }
    void v_n(int a)
    {
        if(razmer!=n)
        {
            na4--;
            if(na4<0)na4=n-1;
            x[na4]=a;
            razmer++;
        }
        else cout<<"dek polon\n";
    }

    void v_k(int b)
    {
        if(razmer!=n)
        {
            kon++;
            if(kon>n-1)kon=0;
            x[kon]=b;
            razmer++;
        }
        else cout<<"dek polon\n";
    }

    int size()
    {
        return razmer;
    }

    void u_n()
    {
        if(razmer!=0)
        {
            na4++;
            if(na4>n-1)na4=0;
            razmer--;
        }
        else cout<<"dek pust\n";
    }

    void u_k()
    {
        if(razmer!=0)
        {
            kon--;
            if(kon<0)kon=n-1;
            razmer--;
        }
        else cout<<"dek pust\n";
    }

    void pe4at()
    {
        int i=na4;
        if(razmer!=0)
        {
            while(1)
            {
                cout << x[i] << "  ";
                if(i==kon)break;
                i++;
                if(i>n-1)i=0;
            }
            cout << "\n";
        }
    }

    dek& operator = (dek const& b)
    {
        if(&b!=this)
        {
            delete [] x;
            x=new int[b.n];
            n=b.n;
            razmer=b.razmer;
            na4=b.na4;
            kon=b.kon;
            if(razmer!=0)
            {

                int i=na4;
                while(1)
                {
                    x[i]=b.x[i];
                    if(i==kon)break;

                    i++;
                    if(i>n-1)i=0;

                }
            }
        }
        return *this;
    }

    dek const operator +(dek const& b)const
    {
        dek s(n+b.n);
        s.n=n+b.n;
        s.razmer=razmer+b.razmer;
        s.na4=0;
        s.kon=s.razmer-1;

        if(razmer!=0)
        {
            int j=0,i=na4;

            while(1)
            {
                s.x[j]=x[i];
                if(i==kon)break;
                i++;
                if(i>n-1)i=0;
                j++;
                if(j>s.n-1)j=0;
            }
        }

        if(b.razmer!=0)
        {
            int j=razmer,i=b.na4;

            while(1)
            {
                s.x[j]=b.x[i];
                if(i==b.kon)break;
                i++;
                if(i>b.n-1)i=0;
                j++;
                if(j>s.n-1)j=0;
            }
        }
        return s;
    }

    dek operator +=(dek const& b)
    {

    }
};

Solution

  • Well, the results of a += b; should be equivalent to a = a + b;; since you have already defined an operator+, you know what these semantics are. Once common practice is to define operator+= first, and then implement operator+ (usually as a free function) in terms of +=:

    MyClass
    operator+( MyClass const& lhs, MyClass const& rhs )
    {
        MyClass results( lhs );
        results += rhs;
        return results;
    }
    

    You then define operator+= to operate directly on the class members:

    MyClass&
    MyClass::operator+=( MyClass const& other )
    {
        n += other.n;
        razmer += other.razmer;
        //  ...
        return *this;
    }
    

    (Although there are good reasons for making it a non-member, traditionally, operator+= is a member. Probably because operator= is required to be a member.)

    Also, traditionally, operator+= returns a reference, because this most resembles the behavior of the operator on built-in types.

    Finally, on a completely different issue: you're missing a copy constructor (which in your case means a double deletion if you do copy), and your operator= is broken (think of what will happen if the x = new int[b.n]; fails and throws an std::bad_alloc). The classical solution for this would be to implement deep copy in the copy constructor (using more or less the same logic you use in setting the variables in your assignment operator), and in the assignment operator, to construct a copy, then swap the elements. This isn't strictly necessary, but whatever you do, you must do the new (and anything else which may fail) before changing the values in the object being assign. (If you do this, the test for self assignment is useless; the need for a test for assignment is usually a signal that the assignment operator is broken.)