Archived

This topic is now archived and is closed to further replies.

EkEk

static int in c++ ????

Recommended Posts

i have this problem. i tried to make a static variabel in class in c++ like in java, like KeyEvent.VK_ESCAPE. so i can use variabel from the class without declare an object.
class A{
  public :
    static int myVar;
};
 
just like that. is it posible if i do something like this
void main(){
  cout << A :: myVar; // or A.myVar
}
 
just like in java KeyEvent.VK_ESCAPE how can i do this?? because above code are error. can someone give a suggestion??

Share this post


Link to post
Share on other sites
You have to initialize it first.

class A
{
public :
static int myVar;
};

// outside the class, possibly in a source (.cpp) file

int A::myVar = 0;

Share this post


Link to post
Share on other sites
a very stupid mistake of mine...
i''ve tried to do like you did
but i forgot to add int in the front

A :: myVar = 1; //without int


thank you very very much....

Share this post


Link to post
Share on other sites