use static variable inside class across instances

 

Hi, 

I want to declare static field inside a class and access it across many instances. ( like java ) . but i see there are some practical limitaions! that I couldn't figure out!!! I know how it can be used in function but i want to use it as static field.

can you tell me how to do so. thanks in advance....

void OnStart()
  {
   testClass t1;
   testClass t2;
   t1.svar = 3;

   Comment("t1: ",t1.svar," | t2: ",t2.svar);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class testClass
  {
public:
   static int        svar;
  };
 
Gholamhossein Eslamizadeh:

Hi, 

I want to declare static field inside a class and access it across many instances. ( like java ) . but i see there are some practical limitaions! that I couldn't figure out!!! I know how it can be used in function but i want to use it as static field.

can you tell me how to do so. thanks in advance....

Read the documentation:  Static Members of a Class 

Static variables are referenced like this:

ClassName::StaticVarName
Documentation on MQL5: Language Basics / Object-Oriented Programming / Static Members of a Class
Documentation on MQL5: Language Basics / Object-Oriented Programming / Static Members of a Class
  • www.mql5.com
Static Members of a Class - Object-Oriented Programming - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Drazen Penic:

Read the documentation:  Static Members of a Class 

Static variables are referenced like this:

thank you @ Drazen PenicXD