0

hello i want to create simple array class to get value and insert value like (array[0] = 4) and this is my program but i have problem to use [] = in same time for insert

template <typename Param>
class arr
{
private:

    int Last_point = 0;
    Param Data[];

public:

    void& operator[]=(int Element_id, Param v)
    {
        Data[Element_id] = v;
    }

    Param& operator[] (int Element_id)
    {
        return Data[Element_id];
    }
};



void main()
{
    arr <int> Array;


    Array[1] = 555;

    cout << "Is(" << to_string(Array[1]) << ")" << endl;

    system("pause");
}

is there any operator like ([]=)? or for this, i have to use which methods? also i want to get value if just used []

Elh48
  • 43
  • 8
  • See [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading). – juanchopanza Dec 21 '16 at 21:39
  • Unrelated to your question, `Param Data[]` is either invalid or makes a pointer. In either case, you'll need to specify a size somewhere. Did you want a fixed size, or were you going to allocate your elements on the heap? In either case, you'll need a proper copy constructor, copy assignment operator, and destructor. – Mooing Duck Dec 21 '16 at 21:44
  • my array maybe have more than 1 million element ... what should i do for default size !!! – Elh48 Dec 21 '16 at 21:46

2 Answers2

2

#include <iostream>
using namespace std;

template <typename Param, size_t Size> class arr {
private:
  Param Data[Size];
public:
  Param &operator[](size_t Element_id) {
    return Data[Element_id];
  }
};

int main() {
  arr<int, 3> Array;
  Array[1] = 555;
  cout << "Is(" << Array[1] << ")" << endl;
}

However, all that arr does in my snippet is be a less useful std::array!

Community
  • 1
  • 1
Jack Deeth
  • 3,062
  • 3
  • 24
  • 39
2

The syntax you are attempting for the operator[] functions is quite wrong. You need something like:

  // Version for non-const objects
  Param& operator[](std::size_t i)
  {
     return Data[i];
  }

  // Version for const objects
  Param const& operator[](std::size_t i) const
  {
     return Data[i];
  }

If you want to support arrays whose sizes are known at compile time, you can use:

template <typename Param, std::size_t N>
class arr
{
   private:

      Param Data[N];

   public:

      Param& operator[](std::size_t i)
      {
         return Data[i];
      }

      Param const& operator[](std::size_t i) const
      {
         return Data[i];
      }
};

If you want to support arrays whose sizes are known at run time, you can use the following. However, you need to be aware of The Rule of Three and make sure to implement the copy constructor and the copy assignment operator properly.

template <typename Param>
class arr
{
   private:

      Param* Data;

   public:

      arr(size_t size) : Data(new Param[size]) {}
      ~arr() { delete [] Data; }

      Param& operator[](std::size_t i)
      {
         return Data[i];
      }

      Param const& operator[](std::size_t i) const
      {
         return Data[i];
      }
};
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270