direct access to hidden variables

J

J. Campbell

In the following example, is there any way to directly access the
"outside" variable, j, from the "inside" scope (after the inside j has
been created)?

Thanks,
Joe

#include<iostream>

using namespace std;
int j = 100; // global j
int main(){
int j = 50; // "outside" j
{
//int j = j/2; // undefined value...both j's are the "inside" j
int j(j/2); // no prob..."inside" j is assigned to "outside" j
cout << " global j = " << ::j << endl
<< " inside j = " << j << endl
<< "outside j = ?\n"; //any way to access "outside" j
//in this scope?
}
system("pause");
return 0;
}
 
G

Gary Labowitz

J. Campbell said:
In the following example, is there any way to directly access the
"outside" variable, j, from the "inside" scope (after the inside j has
been created)?

Thanks,
Joe

#include<iostream>

using namespace std;
int j = 100; // global j
int main(){
int j = 50; // "outside" j
{
//int j = j/2; // undefined value...both j's are the "inside" j
int j(j/2); // no prob..."inside" j is assigned to "outside" j
cout << " global j = " << ::j << endl
<< " inside j = " << j << endl
<< "outside j = ?\n"; //any way to access "outside" j
//in this scope?
}
system("pause");
return 0;
}

NO! Just name it something else. There is rarely a logical reason to name
all those variables the same.

(Of course, if you are really stubborn about it you could place the
variable j of main in its own namespace and refer to it that way. Why do
this?)
 
J

Joe C

Gary Labowitz said:
NO! Just name it something else. There is rarely a logical reason to name
all those variables the same.

(Of course, if you are really stubborn about it you could place the
variable j of main in its own namespace and refer to it that way. Why do
this?)

Thanks Gary. One would, of course never intentionally (ab)use names in this
manner. I was just reading the standard to clairify my understanding of
hidden names, and "discovered" the global namespace resolution operator, and
wondered if there was a way to (directly, eg w/o using namespaces) access a
hidden name in the scope immediately outside the current scope. Your
capitalized answer makes it pretty clear that there is not. ;-)

Thanks for the help-J
 
A

Andrey Tarasevich

J. Campbell said:
In the following example, is there any way to directly access the
"outside" variable, j, from the "inside" scope (after the inside j has
been created)?

No, there's no way.
#include<iostream>

using namespace std;
int j = 100; // global j
int main(){
int j = 50; // "outside" j
{
//int j = j/2; // undefined value...both j's are the "inside" j
int j(j/2); // no prob..."inside" j is assigned to "outside" j

No. In both cases both 'j's are _inside_ (uninitialized) 'j's.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top