Feature request

 

Ive been struggleing with the basic object oriented model given by MQL5 and is continuously keeps me showing its flaws and limitations.

This said, one of the major drawbacks to the OO model given by the MQL5 compiler is the restriction of not being able to return references.

Not only do I see no reason for it to have this limitation, it surely makes it very hard to find certain errors.

As an example, see following code:

struct mydata
{
        int     val1;
        int     val2;

        mydata() :
                val1    (NULL),
                val2    (NULL)
        { };

        mydata operator=(const int p_in)
        { val1 = p_in; return(this); }
}


template <typename T>
struct test
{
        private: 
        T       data_array[];
        int     _size;

        test() :
                _size   (NULL)
        { };

        int resize(const int new_size)
        { return(ArrayResize(data_array)); };

}


test<int> my_obj_A;

my_obj_A.resize(10);
my_obj_A[0] = 3;


test<mydata> my_obj_B;

my_obj_B.resize(5);
my_obj_B[0] = 4;


Sab but its not possible to address or access the object within the array contained by the struct. It will give a copy of the object.

The operator would need following change, but thats not allowed by the compiler:

        T& operator[](const int p_in)
        { return(data_array[p_in]); };



I would like to request this feature as it is very useful.

Any way to do this? Either request the feature or to work around this limitation?

 
Dominik Egert:

Ive been struggleing with the basic object oriented model given by MQL5 and is continuously keeps me showing its flaws and limitations.

This said, one of the major drawbacks to the OO model given by the MQL5 compiler is the restriction of not being able to return references.

Not only do I see no reason for it to have this limitation, it surely makes it very hard to find certain errors.

As an example, see following code:


Sab but its not possible to address or access the object within the array contained by the struct. It will give a copy of the object.

The operator would need following change, but thats not allowed by the compiler:



I would like to request this feature as it is very useful.

Any way to do this? Either request the feature or to work around this limitation?

I would love if this feature gets implemented.
 
Dominik Egert: restriction of not being able to return references.
        mydata operator=(const int p_in)
        { val1 = p_in; return(this); }

Your code does not return a reference; it returns an actual mydata. use pointers:

        mydata* operator=(const int p_in)
        { val1 = p_in; return &this; }
 
Yes, I understand that concept. But it's about the second struct and the [] operator.


 
William Roeder #:

Your code does not return a reference; it returns an actual mydata. use pointers:

There are situations where pointers aren't ideal or possible. I wrote a container similar to C++'s vector class, lets say I write something like this:

array<int> numbers;

numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);

// Won't update the first item since the operator can only return a copy.
numbers[0] = 4;

Being able to return references will improve the language tremendously.

 
Thank you for supporting my case.

That's exactly what I mean.

It is currently almost a useless OO integration, simply because of this possibility missing.

Such a pity.
Reason: