how similar is C++ verses MQL5?

To add comments, please log in or register
antiseptic
106
antiseptic  

If I knew C++ (which I don't) would it be an easy conversion to MQL5?  Like, the same basic rules and syntactical structure?  There is some similarity.  I'm talking generally, I realize that MQL5 is a scripting language built specifically for Metatrader 5, and C++ is an actual programming language.


The main reason I'm asking is that, could I go through a few beginner level videos on youtube on C++ and get a general idea of how MQL5's syntax works?  I understand that specific functions/includes won't work and don't exist in C++ and likely the opposite way around.  I'm coming from Python and the terminology is foreign to me.  In python an array is called a list, I'm thinking if I go through a C++ tutorial or two I'd be able to familiarize myself with the terminology and general structure more efficiently than with some of the beginner guides for MQL5.  Since there doesn't appear to be that much info getting someone from no experience to between beginner and intermediate.

whroeder1
17942
whroeder1  

It's close enough that I'd recommend you do it. Ignore Python. The major differences (besides the functions/includes) off the top of my head are:

  1. MT pointers are handles to a class instance, not an address. You can't increment it to get the next one in an array. You can use a pointer as a reference (no referencing the pointer allowed or required.)
    C/C++
    aClass *pClass = new aClass();
    *pClass.method(); // Dereference the pointer and access the actual object.
    MT4/5
    aClass *pClass = new aClass();
    pClass.method(); // Just access the actual object.

  2. You can't take the address of a class (i.e. &aClass) but must use GetPointer(aClass)

  3. MT templates are more rudimentary, Argument deduction as used in the C++'s STL won't work in MT. (Yet?) SFINAE - cppreference.com

  4. MT, now, automatically generates a assignment operator. C++ doesn't.
    MetaTrader 5 Platform Beta Build 1625: Custom financial instruments - General - MQL5 programming forum

  5. Classes can be derived only from classes and always have a virtual function table (for destructor.) Structures can be derived only from structures; no virtual functions. Structures, Classes and Interfaces - Data Types - Language Basics - MQL4 Reference

  6. There is a keyword interface, I ignore it. It's syntax sugar, not functional.

  7. There is a keyword typedef. Only works as a pointer to a method. Still have to use #define for all others.

  8. There is a keyword const. Which doesn't work properly with pointers (const aClass *pcClass vs aClass* const CPncClass) and casting a const subclass pointer to a const/non-const base class doesn't compile. I "#define CONST // Nothing", so I can eventually replace them if/when that becomes fixed.

  9. There is no mutable keyword.

  10. Operator Overloading can't be standalone functions (yet) Ticket #1255686 | 2015.07.08 21:05

  11. There are no exceptions.

  12. A Character (char) and a short are both 16 bits, not 8 bit bytes.
Obviously since you don't know C++ most of that you won't understand. Refer to it as you learn new concepts.
antiseptic
106
antiseptic  
Thanks for all the information. 
whroeder1
17942
whroeder1  
whroeder1: № 2 You can't take the address of a class (i.e. &aClass) but must use GetPointer(aClass)
v* p = &c; // is the same as GetPointer.
Alain Verleyen
36131
Alain Verleyen  
If you are studying modern C++ (C++11 and further) almost nothing will be available in MQL. If the goal is to apply your C++ study to MQL, you should rather stick to C++98.
Icham Aidibe
11287
Icham Aidibe  
antiseptic:

If I knew C++ (which I don't) would it be an easy conversion to MQL5?  Like, the same basic rules and syntactical structure?  There is some similarity.  I'm talking generally, I realize that MQL5 is a scripting language built specifically for Metatrader 5, and C++ is an actual programming language.


The main reason I'm asking is that, could I go through a few beginner level videos on youtube on C++ and get a general idea of how MQL5's syntax works?  I understand that specific functions/includes won't work and don't exist in C++ and likely the opposite way around.  I'm coming from Python and the terminology is foreign to me.  In python an array is called a list, I'm thinking if I go through a C++ tutorial or two I'd be able to familiarize myself with the terminology and general structure more efficiently than with some of the beginner guides for MQL5.  Since there doesn't appear to be that much info getting someone from no experience to between beginner and intermediate.

I'm not a professional programmer, I learn a language when I need something for myself. For a ios app, if there's no alternative, I'll learn the swift language just enough to be able to get what I want etc ... 

And I have to say that MQL isn't difficult that much to handle, and it's convenient when it comes to stay focus on the underlying strategy rather than pure programming. 

So it goes without saying that the more you're mastering the language the easier it is to express your idea, and so your ea/script/app or whatever. 

<edited by moderator>
To add comments, please log in or register