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.

  • 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. – doug Jun 18 '19 at 06: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. – user4581301 Jun 18 '19 at 06:28
  • Oh wait there it is down in `CreateArray`. I recommend a read of [What is meant by Resource Acquisition is Initialization (RAII)?](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii). It will help you write more robust code that is actually simpler. – user4581301 Jun 18 '19 at 06:30
  • Much appreciated, will definitely give it a read :) – Leith Merrifield Jun 18 '19 at 06:35

1 Answers1

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.

Oblivion
  • 6,320
  • 2
  • 9
  • 30
  • Thanks, plan to add range checking, however changing the ```T*``` to ```T&``` still shows ```no suitable conversion function from DynArray to int exists``` – Leith Merrifield Jun 18 '19 at 06:22
  • Its because your variable is a pointer and the compiler tries to interpret your variable as array. Try without heap allocation (new) – Narase Jun 18 '19 at 06:31
  • 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.](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). Give `int main() { DynArray myArray; myArray.CreateArray(); int test = myArray[2]; return 0; }` a try instead. – user4581301 Jun 18 '19 at 06:46
  • @user4581301 he already removed pointer. I add to the answer though. – Oblivion Jun 18 '19 at 06:49