1

I'm trying to overload the [] operator in a templated dynamic array, however it doesn't seem to be doing anything?

I created a templated dynamic array for school, I've tried separating the overload to outside the class.

The DynArray.h

template <typename T>
class DynArray
{
public:
    //The constructor initialises the size of 10 and m_Data to nullptr
    DynArray(void)
    {
        m_AllocatedSize = 10;
        m_Data = nullptr;
    }

    //deletes m_Data
    ~DynArray()
    {
        delete[] m_Data;
        m_Data = nullptr;
    }

    T* operator [] (int index)
    {
        return m_Data[index];
    }

    //creates the array and sets all values to 0
    T* CreateArray(void)
    {
        m_Data = new T[m_AllocatedSize];
        m_UsedElements = 0;

        for (int i = 0; i < m_AllocatedSize; ++i)
        {
            m_Data[i] = NULL;
        }

        return m_Data;
    }

private:

    bool Compare(T a, T b)
    {
        if (a > b)
            return true;
        return false;
    }


    T* m_Data;
    T* m_newData;
    int m_AllocatedSize;
    int m_UsedElements;
};

Main.cpp

#include <iostream>
#include "DynArray.h"
int main()
{
    DynArray<int>* myArray = new DynArray<int>;
    //runs the create function
    myArray->CreateArray();

    int test = myArray[2];

    delete myArray;
    return 0;
}

I expected the overload to return in this case the int at m_Data[2], however it doesn't seem to overload the [] at all instead says no suitable conversion from DynArray<int> to int.

4
  • You are creating a pointer to a single DynArray then accessing the third one which doesn't exist. You can use (*myArray)[2] to access the pointer returned by [] but keep in mind, it's a pointer and probably not what you want. Commented Jun 18, 2019 at 6:24
  • m_AllocatedSize = 10; along with m_Data = nullptr; doesn't seem right. If the allocated size is ten, without allocating anything it looks like you're setting up for fail. Commented Jun 18, 2019 at 6:28
  • Oh wait there it is down in CreateArray. I recommend a read of What is meant by Resource Acquisition is Initialization (RAII)?. It will help you write more robust code that is actually simpler. Commented Jun 18, 2019 at 6:30
  • Much appreciated, will definitely give it a read :) Commented Jun 18, 2019 at 6:35

1 Answer 1

1

You are returning a pointer which is not what you want. You should do like this:

T& operator [] (const int& index)
 { 
    return   m_Data[index];
 }

Also myArray is a pointer you have to dereference it before using.

int test = (*myArray)[2];

It's better to not to use pointer:

    int main()// suggested by @user4581301
{
    DynArray<int> myArray;
    //runs the create function
    myArray.CreateArray();

    int test = myArray[2];


    return 0;
}

There is no reason for using pointers here.

Instead of new and delete for dynamic allocation it is better to use smart pointers. There is also one issue here, you are not chacking the range and what if theindex was for example a negative number.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, plan to add range checking, however changing the T* to T& still shows no suitable conversion function from DynArray<int> to int exists
Its because your variable is a pointer and the compiler tries to interpret your variable as array. Try without heap allocation (new)
It's still a pointer. This is not a good place to use a dynamic allocation. Save new for times you really need it. That's almost never.. Give int main() { DynArray<int> myArray; myArray.CreateArray(); int test = myArray[2]; return 0; } a try instead.
@user4581301 he already removed pointer. I add to the answer though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.