How to overload the subscripting assignment operator '[]=' ?

 

Hi all,

the example is generic, since I have no idea how to overload the indexed assignment ... does anyone know how to do?

class CIntArray
        {
        int array[];
        ...
        public: void Set(int index, int value)
                {
                array[index]=value;
                }
        public: int Get(int index)
                {
                return array[index];
                }
        public: int operator [] (int index)                     // Works
                { 
                return Get(index); 
                }       
        public: void operator [] (int index, int data)  // Doesn´t
                {
                Set(index,data);
                }
        };

CIntArray myarray;
int n=myarray[0];	// Leads to Get()
myarray[0]=100;		// Implementation missing
 
Doerk Hilger: Hi all, the example is generic, since I have no idea how to overload the indexed assignment ... does anyone know how to do?

As far as I know there is no "Set" variation for the indexing [] operator overloading, only for "Get".

In fact I remember reading a forum post of a work around several years back. I will see if can find it and reference it here.

 

I found one, but I'm not sure if this is the one read back then.

Forum on trading, automated trading systems and testing trading strategies

Custom indexing of an array / subscript operation overloading

nicholish en, 2018.06.23 20:01

You can't do assignment like that since you're returning an int, which is the equivalent to 5 = 7;

In order to implement assignment in the way you're attempting you'll need to return a reference to an object whose assignment operator has also been overloaded.  ...which might look something like this. 


#include <Arrays\List.mqh>

class IntWrapper : public CObject
{
public:
   int custom_index;
   int num;
   void operator = (int n) { num = n; }
};

class MyIntList : public CList
{
public:
   IntWrapper *operator[](int i)
   {
      IntWrapper *iw = this.GetFirstNode();
      for(; CheckPointer(iw); iw=iw.Next())
         if(iw.custom_index == i)
            return iw;
      iw = new IntWrapper();
      this.Add(iw);
      iw.custom_index = i;
      return iw;
   }
};


void OnStart()
{
   MyIntList list;
   
   list[900] = 25;
   list[5] = 1900;
   
   Print(list[900].num);

}

 
MQL's OOP notes: Arrayed indicator buffers based on operators overloading
MQL's OOP notes: Arrayed indicator buffers based on operators overloading
  • 2016.09.17
  • www.mql5.com
From very first moment as I started learning MetaTrader many years ago I was wondering why there are no multidimentional buffers in indicators. Indeed, when you code an indicator with multiple buffers
 
overloading with the same parameter-set ? - How to overload operators and functions on both sides of the =-Assignment
overloading with the same parameter-set ? - How to overload operators and functions on both sides of the =-Assignment
  • 2016.04.04
  • www.mql5.com
Code (not working because of: ''operator[]' - function already defined and has different type" you can overload operators and functions (they must have different parameters. If i overload the operator[], it has to be usable on both sides of the =-assignment
 

Fernando beat me to it as usual . There is no "void" operator that can be overloaded so you lose the get shortcut with this 

template <typename X>
class xElement{
public:
X data;
X get(){return(data);}
void operator=(X value){data=value;}
};
class CIntArray
        {
        //int array[];
        public:
        xElement<int> array[];
        public: 
        public: void Set(int index, int value)
                {
                array[index]=value;
                }
        public: int Get(int index)
                {
                return array[index].get();
                }
        public: xElement<int>* operator [] (int index)                     // Works
                { 
                if(index>=0&&index<ArraySize(array)){
                return(GetPointer(array[index]));}
                else{return(NULL);}
                }       
        };


int OnInit()
  {
//--- create timer
CIntArray myarray;
ArrayResize(myarray.array,1,0);
myarray[0]=100;         // Implementation missing
//int retriever=myarray[0];
Print("myarray.array[0].data="+myarray.array[0].data);
   
//---
   return(INIT_SUCCEEDED);
  }
 

Thx guys.


Fernando Carreiro #:

I found one, but I'm not sure if this is the one read back then.


Look like an interesting idea, will have a closed look if this can lead to what I am actually searching for.

 
Doerk Hilger #: Thx guys.

You are welcome!

Reason: