Typing question - page 4

 
Ilya Malev:

It's not very clear what the problem is. Couldn't the object initialization be placed in a separate method like Init(), maybe even a virtual one?

Yes, of course. Couldn't class.get_int()/class.get_double()/class.get_string()/... ?)). Style, habit ...

 
pavlick_:

Of course, you can. Couldn't class.get_int()/class.get_double()/class.get_string()/. ?)). Style, habit ...

We can, but let's write d=var[x].get_double instead of d=var[x] wherever there is double d, var[];. If we're going to engage in masochism, we should do it collectively =))

 
Ilya Malev:

So, in the end it comes down to the wish to introduce in mql the ability to overload the typing operation (including implicit), i.e. define a method call context and depending on expected in this context return value type to call the required code.

This is obviously not a C++ standard, and in general, the good thing about C++ (and it was taken by MQL developers as a basis) is that everything in C++ that the programmer couldn't handle explicitly, he had to work with the pointers and to write histypedef type

a pointer is just a pointer to a memory address, you can always pass it as a parameter and get it as a result of a function, dereferencing a pointer gives you a physical value in memory bytes, converting a pointer to its new type gives you lots of bytes of memory ))))

In MQL, the pointers are hard to say why, but they are there, the developers said the priority is data protection, so all the innovations that could lead to sandbox exits are excluded



on the subject, unfortunately I have little practice working with templates ( template ), but I suspect that you can do so:

1. write some overloaded functions that return the required type as a result, and take that type as a parameter, i.e. like this:

//+------------------------------------------------------------------+
int f(const int x)
  {
   return((int)1);
  };
//+------------------------------------------------------------------+
double f(const double x)
  {
   return((double)2);
  }
//+------------------------------------------------------------------+
string f(const string x)
  {
   return((string)3);
  }
//+------------------------------------------------------------------+
void OnStart()
  { 
   int    a=0;
   double b=0;
   string c="0";
   a = f(a);
   b = f(b);
   c = f(c);
   Print("a = ",a);
   Print("b = ",b);
   Print("c = ",c);
}

2. now the f() function needs to be wrapped in a template and hide the parameter x - if templates allow you to do so, then the call will be a=f() - visually everything will be nice, like typing

 
Igor Makanu:

it's obviously not a C++ standard

I think you're wrong - although I haven't written in "pure" C++ for a long time, there are plenty of code examples like this on the web

class A
{
public:
    operator int()const;
};

A::operator int()const
{
    return 0;
}
 
Ilya Malev:

I think you're wrong - although I haven't written in "pure" C++ for a long time, but the web is full of code examples like this

But what if it's a complex type? - A structure or an array? In C++ this was solved by using pointers to their type, physically the compiler returned the memory address and dereferencing the pointer ensured that the data was handled correctly

 
Igor Makanu:

This example would work correctly, but what if it's a complex type? - In C++ this was solved with pointers to its type, physically the compiler returned the memory address and the pointer dereferencing provided the correct handling of the data

Such an example wouldn't work correctly in mql, unfortunately.

A complex type, structure or array can overload whatever you need, it doesn't require type conversion overloading...

 
Ilya Malev:

In mql such example would not work correctly, unfortunately.

Well, yes, I just saw, you want to overload type conversion, MQL help clearly says that it's allowed to overload, and unary operations are overloaded only as unary and binary, respectively, I was working with matrices, tried to overload ^ - it doesn't work, I had to use !

  • бинарные +,-,/,*,%,<<,>>,==,!=,<,>,<=,>=,=,+=,-=,/=,*=,%=,&=,|=,^=,<<=,>>=,&&,||,&,|,^;
  • unary +,-,++,--,~;
  • assignment operator =;
  • indexing operator [].


I looked again at my example - the compiler will not allow to wrap my example ina template. I can return((1/x) - and I can give a string string as a parameter x. Usually all C compilers at the compile time check for type matching, and MQL does not allow to wrap ambiguous solutions in templates.

imho, your problem - post 1 of the topic, in MQL can be solved correctly, only by describing alloverloaded functions. Then, all types of variables which are passed and returned and all overloaded functions will be checked at compile time.

 
Igor Makanu:

Yes, I just saw, you want to overload type conversion, MQL help clearly says that overloading is allowed, and unary operations are overloaded only as unary and binary respectively, I was working with matrices, tried to overload ^ - it does not work, I had to use !

I can state for 99% that there is no such a situation where ^ cannot overload, while ! And overloading capability doesn't depend on the type of operator. You must have misunderstood something. Just post this example here, but if you've already forgotten and forget about it, you'd better not.)

The only limitation I've encountered concerning operator type is a ban on overloading of logical operators ==, !=, ! and = as applied to pointers (any_type * ). Overloading them correctly requires either working with autobjects or structures. Just in recent months I've eaten a lot of dogs on these things, so I can speak confidently :)

 
Ilya Malev:

I can say for 99% that there is no situation where ^ does not overload, but ! does. And the ability to overload does not depend on the type of operator. You must have misunderstood something. Just post this example here, but if you've already forgotten and forget about it, you'd better not.)

The only limitation I've encountered concerning operator type is a ban on overloading of logical operators ==, !=, ! and = as applied to pointers (any_type * ). Overloading them correctly requires either working with autobjects or structures. In recent months I've eaten a lot of these tricks, so I can confidently say :)

#include <Math\Alglib\linalg.mqh>
class Matrix
  {
public:
   CMatrixDouble     M;
   int               row;//m_strok;
   int               col;//n_stolb;
   void              Matrix(void)         {                             }
   void              Matrix(int m,int n)  { M.Resize(m,n); row=m;col=n; }
   //----      умножение матриц
   Matrix operator*(const Matrix &B){Matrix res(this.row,B.col);CAblas::RMatrixGemm(this.row,B.col,this.col,1.0,this.M,0,0,0,B.M,0,0,0,0,res.M,0,0);return(res);  }
   //----      транспонирование матрицы
   Matrix operator!(void){Matrix res(this.col,this.row);CAblas::RMatrixTranspose(this.row,this.col,this.M,0,0,res.M,0,0);return(res);                             }
   //----      нулевая матрица
   void              zeros(int r,int c) {this.M.Resize(r,c);this.row=r;this.col=c;for(int i=0;i<r;i++){for(int j=0;j<c;j++){this.M[i].Set(j,0.0);}}               }
   //----      вывод в журнал матрицы
   void MatrixPrint(string  separator="|",uint digits=3){string s,sep=" "+separator+" ";for(int i=0;i<row;i++){s=separator;for(int j=0;j<col;j++) s+=DoubleToString(M[i][j],digits)+sep;Print(s);Sleep(123);}}
private:
   void              Matrix(const Matrix &R) { this=R;                                                                                                            }
  };

In "classic" matrix handling you need to use ^ operator to transpose matrices, here's my example - I ported SSA method from Matlab, it's the simplest multiplication, assignment and transposition of matrices based on CMatrixDouble - it ... it doesn't know the size of matrices it stores (how many rows and columns it has).

 
P.S., ah, i.e. you wanted to overload a binary operator as a unary one (2ar as 1ar), then yes, of course it won't. The only exception is []
Reason: