Creating and initializing public variable

Hello!

Assume I have a code like this:

static void main() {

}

int test() {

}

I want to define a variable in this code which can be accessible from main and int.

Also if I define a variable for example X in main and another X in test, how can I call test's X from main?

Thanks
LVL 17
CSecurityAsked:
Who is Participating?
 
rajeev_devinConnect With a Mentor Commented:
>> But I heard something like test::X or something like this
This is possible with static class members.
Or, namespace...

Example:

namespace test {
      int X;
};

static void main() {

      test::X = 0;
}
0
 
xRalfConnect With a Mentor Commented:
Hi,

define the variable as global before main
e.g.

int x;
static void main() {

}

int test() {

}

If you define the variable X in test() it will be local variable for test() and it is impossible to call it from main. It's allocated on the stack and exists only during the function call.
0
 
CSecurityAuthor Commented:
But I heard something like test::X or something like this
0
 
CSecurityAuthor Commented:
is it possible?
0
 
rajeev_devinCommented:
>> is it possible?
No it is not possible
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.