static variable in objects !!

 

Hi

can anybody tell : why all function in different object behave like they have the same static variable ?

the result is only : div

thank you in advanced


class sweep
  {
protected:
private:

public:
                     sweep() {} ;
                    ~sweep() {};
   bool              IsNewCandle();
   virtual void      check_fun() {};
  };

//+------------------------------------------------------------------+
bool sweep::IsNewCandle()
  {
   static datetime lasttime;

   if(iTime(NULL,PERIOD_M5,0)==lasttime)
      return false;
   else
      lasttime=iTime(NULL,PERIOD_M5,0);
   return true;
  }
//+------------------------------------------------------------------+
class sweepDiv :public sweep
  {
public:
                     sweepDiv()     {};//Constructor
   virtual void              check_fun()
     {
      Print("sweepDiv ");
     }
  };
//+------------------------------------------------------------------+
class sweepTrigger :public sweep
  {
public:
                     sweepTrigger() {};
   virtual void      check_fun()
     {
      Print("trigger ");
     }
  };

sweepDiv  swdiv();                // divergence
sweepTrigger  swtr();          // trigger
sweepDiv  swdiv2();
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetTimer(3);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   if(swdiv.IsNewCandle())
     {
      printf("div");
     }
   if(swdiv2.IsNewCandle())
     {
      printf("div2");
     }
   if(swtr.IsNewCandle())
     {
      printf("triger");
     }
  }

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

...

 
Your topic has been moved to the section: MQL4 e MetaTrader 4 — In the future, please consider which section is most appropriate for your query.
 
Mehrdad Sarrafi:

Hi

can anybody tell : why all function in different object behave like they have the same static variable ?

the result is only : div

thank you in advanced

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Hover your mouse over your post and select "edit" ... 

...


Your code is missing, so I'll take a general approach.

Static variables have a lifetime, it is defined by the lifetime of your program.

A static variable has a singleton behaviour throughout it's visual scope. No matter where it is declared and initialized, you will always have only one copy of it's content.



 
Dominik Christian Egert #:

Your code is missing, so I'll take a general approach.

Static variables have a lifetime, it is defined by the lifetime of your program.

A static variable has a singleton behaviour throughout it's visual scope. No matter where it is declared and initialized, you will always have only one copy of it's content.



yes , I added the code again in appropriate format

I expect that static variables in different objects , considered as different variables . and then...

but it seems that all of the variables in different objects refer to one variable !!!

would you give me a suggestion to implement my idea.

I want to run my virtual function , that is different in every class , only one time in a candle through my magic IsNewCandle() function.

thank you

 
Mehrdad Sarrafi #:

yes , I added the code again in appropriate format

I expect that static variables in different objects , considered as different variables . and then...

but it seems that all of the variables in different objects refer to one variable !!!

would you give me a suggestion to implement my idea.

I want to run my virtual function , that is different in every class , only one time in a candle through my magic IsNewCandle() function.

thank you

Declare the variable in the class object, not on function level. Do not declare it as static.


 
Dominik Christian Egert #:
Declare the variable in the class object, not on function level. Do not declare it as static.


not declare as static!!

I think it called clearing the problem , not solving it !!

because this idea need static variable to implement


also

by declaring in class level , I receive  "unresolved static variable" error

 
Mehrdad Sarrafi #: by declaring in class level , I receive  "unresolved static variable" error

You didn't read the documentations fully.

class C{
   static Type sv; //initialValue
}
static Type C::sv=initialValue;
 
Mehrdad Sarrafi #:

not declare as static!!

I think it called clearing the problem , not solving it !!

because this idea need static variable to implement


also

by declaring in class level , I receive  "unresolved static variable" error

May I ask, how often do you create your sweep* objects??

If you keep creating them over and over again, you will need to externalize this functionality, as NewBar relying on a static existence.

But if you create the object only once, then do not declare the variable static, as it continues to exist in the class object for the lifetime of the class objects scope.

Hope you understand.

EDIT:

static members require external initialization. It is not enough to add a static declaration to the class members.



 
William Roeder #:

You didn't read the documentations fully.

Ha, yes... Although I doubt T will be understood....

Let's see and wait :-)
 


static members require external initialization. It is not enough to add a static declaration to the class members.



really I have not read this part at all !

clearly mentioned:

"These data members are shared by all instances of this class and are stored in one place. Non-static data members are created for each class object variable."

thank you

 
Dominik Christian Egert #:
Declare the variable in the class object, not on function level. Do not declare it as static.


as it continues to exist in the class object for the lifetime of the class objects scope.

thank you very much

problem solved!

Reason: