static - C++: save variable value for next call of the function -


is there way initialize variable in function , save value next call of function?

i'm making application in qt , have 1 function connected signal. want variable in function change after other 1 reaches goal. here body of function:

void objekt::advance(int phase) { if(!phase) return;  qpointf location = this->pos(); if (int(location.x())==200 || int(location.x())==-200) {     smijer=-smijer;  } setpos(maptoparent(smijer,0)); } 

i defined smijer variable static int. dont'know how initialize once, when program starts, , how keep new value after each call of function.

your answer in question basically. static variables (either class member or local variable of function) initialized once terminated. example;

#include <iostream> int foo () {    static int svar = 5;    svar++;    return svar; }  using namespace std; int main () {    int iter = 0;    {        cout << "svar :" foo() << endl;        iter++;       }while (iter < 3);  }  

if write program print out svar values like;

svar :6 svar :7 svar :8 

so see although call foo function 3 times initialization of static varible done once.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -