Solved

Creating and initializing public variable

Posted on 2006-11-22
5
219 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:CSecurity
  • 2
  • 2
5 Comments
 
LVL 6

Assisted Solution

by:xRalf
xRalf earned 250 total points
ID: 17994202
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
 
LVL 17

Author Comment

by:CSecurity
ID: 17994206
But I heard something like test::X or something like this
0
 
LVL 17

Author Comment

by:CSecurity
ID: 17994252
is it possible?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17994295
>> is it possible?
No it is not possible
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 250 total points
ID: 17994307
>> 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

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question