Archived

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

static int in c++ ????

This topic is 5093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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