Solved

Creating and initializing public variable

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now