Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Singleton Example - Expert for MetaTrader 4 - MT4/MT5 Resources

author EAcpu | 3 reads | 0 comments |

Singleton Example - expert for MetaTrader 4

Here is a working singleton code example. The sole purpose of this article is to provide working code examples of singleton objects. Singleton objects are created when "there can only be one" object. This is accomplished by making the object's constructor private and ensuring that assignment and copy operations are also private.

I was developing an EA and discovered that there shouldn't be multiple copies of an object. I don't want the EA to get big enough that using singletons actually matters, but I like the clarity of object type code. Some programmers object to using singletons, but I think the technical reasons for not using them can mostly be solved with newer compiler technology. No one seems to have provided an example of this functionality in this environment - I've only found old references in the code base from before the compiler supported private static variable initialization - as you can see in this example, this functionality works fine now.

Comments or contributions to improve the code are always welcome.

Singleton Example - expert for MetaTrader 4


Attachment download

📎 singletonexample.mq4 (5.49 KB)

Source: MQL5 #14220

Verification code Refresh