0

I'm currently trying to overload the + operator in order to combine two arrays of type T, but I've been hitting a wall for the last hour or so . I want to do this without making any use of stl because I'm a beginner in C++ and I want to get a good grip on implementing classes before using the standard ones .

The context would be that I'm currently designing a vector class of my own by using a templated dynamically allocated array.

Therefore, what I'm interested in at this point is overloading the + operator so that when performing c = a + b inside the main function , where a , b , c are Vector<T> objects, c would become the combination of those two ( concatenation ) .

I can't really wrap my mind around this, since the function that defines the behaviour of the operator can handle at most one parameter .

Can anyone suggest any ideas ?

user43389
  • 721
  • 6
  • 18
  • 1
    `The context would be that I'm currently designing a vector class of my own by using a templated dynamically allocated array.` I will just tell you that I have yet seen a beginner write such a class correctly without loads of help from experienced programmers. – PaulMcKenzie Mar 28 '15 at 01:50
  • 1
    `what I'm interested in at this point is overloading the + operator so that when performing c = a + b` Overload `+=` first, then implement `operator+` in terms of `+=`. And at the very least, post your code. As my previous comment stated, there is a good chance you're doing things fundamentally wrong. – PaulMcKenzie Mar 28 '15 at 01:52
  • The other parameter is the invoking object itself. So a + b is read as a.operator+(const T & b). – itachi Mar 28 '15 at 01:57
  • 1
    [Read this](http://stackoverflow.com/questions/4421706/operator-overloading) – M.M Mar 28 '15 at 01:58

1 Answers1

1

Try this:

template<typename T>
class Vector
{
private:
    T *m_array;
    int m_count;

public:
    Vector()
        : m_array(NULL), m_count(0)
    {
    }

    Vector(const Vector &src)
        : m_array(NULL), m_count(0)
    {
        T* new_array = new T[src.m_count];
        for (int x = 0; x < src.m_count; ++x)
            new_array[x] = src.m_array[x];

        m_array = new_array;
        m_count = src.m_count;
    }

    ~Vector()
    {
        delete[] m_array;
    }

    // mutilator and accessor functions ...

    Vector& operator=(const Vector &rhs)
    {
        if (this != &rhs)
        {
            T* new_array = new T[rhs.m_count];
            for (int x = 0; x < rhs.m_count; ++x)
                new_array[x] = rhs.m_array[x];

            T* old_array = m_array;
            m_array = new_array;
            m_count = rhs.m_count;

            delete[] old_array;
        }

        return *this;
    }

    Vector& operator+=(const Vector &rhs)
    {
        int new_count = m_count + rhs.m_count;
        T* new_array = new T[new_count];

        for (int x = 0; x < m_count; ++x)
            new_array[x] = m_array[x];
        for (int x = 0; x < rhs.m_count; ++x)
            new_array[m_count+x] = rhs.m_array[x];

        T* old_array = m_array;
        m_array = new_array;
        m_count = new_count;

        delete[] old_array;

        return *this;
    }
};

template<typename T>
Vector operator+(const Vector<T> &lhs, const Vector<T> &rhs)
{
    // if you want to optimize this further, make this operator
    // a 'friend' of the Vector class so it can access the
    // m_array and m_count fields directly, then it can perform
    // one allocation+copy instead of two...

    Vector<T> v(lhs);
    v += rhs;
    return v;
}

Vector a, b, c;
// add items to a and b ...
c = a + b;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    I would write `operator=` using copy/swap. – PaulMcKenzie Mar 28 '15 at 02:20
  • @PaulMcKenzie: So would I (if not using `std::vector`), but the OP specifically stated: "*I want to do this **without making any use of stl** because I'm a beginner in C++ and I want to get a good grip on implementing classes before using the standard ones.*". – Remy Lebeau Mar 28 '15 at 02:21
  • 1
    You don't need "STL" to do copy/swap. Only a swap function is needed. If you really don't want to use `std::swap` it is very easy to write your own code to swap two pointers. – M.M Mar 28 '15 at 02:29
  • Thanks a lot for your incredibly clear code, you provided an answer that helped me on a much deeper level than I was hoping for . My question is, say, in this particular snippet `:Vector(const Vector &src) : m_array(NULL), m_count(0)` , how does this work ? Precisely, how is it possible to write code between the signature of the function and the actual code block ? Furthermore `m_array(NULL), m_count(0)`, I got how those work, but I've never seen syntax like this either . I hope I'm not too vague and if possible could you suggest any references/materials ? Thanks a lot again ! – user43389 Mar 28 '15 at 23:52
  • @user43389: in a constructor, the code between the `:` and `{` is called the [member initializer list](http://en.cppreference.com/w/cpp/language/initializer_list). It is used to call base class constructors and initialize member fields before the body of the constructor begins running. – Remy Lebeau Mar 29 '15 at 00:27