Solved

Creating and initializing public variable

Posted on 2006-11-22
5
225 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 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