Ziheng Zhuang:
Will MQL not support the casting of simple struct ?
https://www.mql5.com/en/docs/basis/types/casting#casting_structure
Struct casting is a basic feature in MQL, but now it does not work.
Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий
Особенности языка mql5, тонкости и приёмы работы
fxsaber, 2017.04.27 13:38
template <typename T> class CASTING { public: template <typename T1> static const T Casting( const T1 &Value ) { union CAST { T1 Value1; const T Value2; CAST( const T1 &Value) { this.Value1 = Value; // кастомный оператор может все испортить } }; const CAST Union(Value); return(Union.Value2); } }; #define _C(A, B) CASTING<A>::Casting(B)
// MetaEdit: v5.0,build 1596. April 26,2017 // MT5: v5.0,build 1596. April 26,2017 // script: test_structCast.mq5 struct ID { int num; //4 ushort name[255];//255*2=510 SIZEOF(ID) = 514 }; struct ID2 { ushort id[2+255];//257 *2 = 514 }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- ID id; id.num = 1234; string s = "someone"; int cnt = StringToShortArray(s,id.name); if(cnt<=0)return; //--- ID2 id2; id2 = _C(ID2, id); // id2 = (ID2)id; //--- } //+------------------------------------------------------------------+Look here.
Thank you fxsaber, you did a great job.
Now it works well.
// MetaEdit: v5.0,build 1596. April 26,2017 // MT5: v5.0,build 1596. April 26,2017 // script: test_structCast.mq5 #include <TypeToBytes.mqh> struct ID { int num; //4 ushort name[255];//255*2=510 SIZEOF(ID) = 514 }; struct ID2 { ushort id[2+255];//257 *2 = 514 }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- ID id; id.num = 1234; string s = "someone"; int cnt = StringToShortArray(s,id.name); if(cnt<=0)return; //--- ID2 id2; // id2 = (ID2)id; // Error: cannot cast "ID" to ID2 //--- //---https://www.mql5.com/ru/code/16280 id2 = _C(ID2,id); ID idx; idx = _C(ID,id2); printf("idx.num = %d, idx.name = %s",idx.num,ShortArrayToString(idx.name)); // 1234,someone } //+------------------------------------------------------------------+
Ziheng Zhuang:
// MetaEdit: v5.0,build 1596. April 26,2017 // MT5: v5.0,build 1596. April 26,2017 // script: test_structCast.mq5 #include <TypeToBytes.mqh> struct ID { int num; //4 ushort name[255];//255*2=510 SIZEOF(ID) = 514 }; struct ID2 { ushort id[2+255];//257 *2 = 514 }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- ID id; id.num = 1234; string s = "someone"; int cnt = StringToShortArray(s,id.name); if(cnt<=0)return; //--- ID2 id2 = {0}; // id2 = (ID2)id; // Error: cannot cast "ID" to ID2 //--- //---https://www.mql5.com/ru/code/16280 // id2 = _C(ID2,id); _W(id2) = id; ID idx = {0}; // idx = _C(ID,id2); _W(idx) = id2; printf("idx.num = %d, idx.name = %s",idx.num,ShortArrayToString(idx.name)); // 1234,someone if ((_R(id2) == idx) && (_R(id) == idx)) Print("OK!"); } //+------------------------------------------------------------------+
fxsaber:
Great!
It is worth taking time to study this library.
Thank you.
Ziheng Zhuang:
It is worth taking time to study this library.
#include <TypeToBytes.mqh> struct ID { int num; //4 uchar name[255]; }; struct ID2 { uchar id[sizeof(ID)]; }; void OnStart() { ID id = {0}; _W(id) = 1234; _W(id)[_OFFSET(id, name)] = "someone"; //--- ID2 id2 = {0}; _W(id2) = id; ID idx = {0}; _W(idx) = id2; printf("idx.num = %d, idx.name = %s",idx.num,CharArrayToString(idx.name)); // 1234,someone }
Great job!
Thank you.
Thank you.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Will MQL not support the casting of simple struct ?
https://www.mql5.com/en/docs/basis/types/casting#casting_structure
Struct casting is a basic feature in MQL, but now it does not work.
Why MQL disable this featue?