By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

direct access to hidden variables

P: n/a
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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
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?)
--
Gary
Jul 22 '05 #2

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:ls********************@comcast.com...
"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
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?)
--
Gary


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
Jul 22 '05 #3

P: n/a
J. Campbell wrote:
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.
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;
}


--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.