Reputation: 225
My code :
int num = 1; // global scope
int main(){
int num = 2; // local scope 1
{ // local scope 2
int num = 3;
{ // local scope 3
int num = 4;
std::cout<<num<<"\n"; // printing local scope 3
std::cout<<::num<<"\n"; // printing global scop
// but here how to print local scope 1, 2 variables
}
}
Guys in my code there are nested scopes and I want to print all variables with same name, including shadowed ones, from "local scope 3". However, I can print global and local scope 3's num's value but I don't know the syntax for accessing the local scopes 1 and 2's num's value.
Upvotes: 1
Views: 386
Reputation: 38947
It's not entirely clear what you want exactly. You use the word recursion, but you are not using recursion.
However, I think what you are describing and want would suit a recursive function. Maybe something like the following do what you want?
#include <iostream>
void printnum(int num, int max)
{
if (num < max)
printnum(num+1, max);
std::cout<<num<<"\n";
}
int main(){
printnum(1, 3);
return 0;
}
Upvotes: 0
Reputation: 7915
As the user jasmeet pointed out, you simply cannot do that directly. However, indirectly there is a workaround to this but it would call for the use of a container...
#include <iostream>
#include <vector>
int num = 1;
int main() {
std::vector<int> results;
results.push_back(num);
int num = 2;
results.push_back(num);
{
int num = 3;
results.push_back(num);
{
int num = 4;
results.push_back(num);
for (auto& v : results)
std::cout << v << " ";
std::cout << '\n';
}
}
return 0;
}
Output
1 2 3 4
Upvotes: 1
Reputation: 107
just use in your scope:
int num = 1; // global scope
int main() {
int num = 2;
{
int num = 3;
{
int num = 4;
std::cout << num << '\n';
}
std::cout << num << '\n';
}
std::cout << num << '\n' << ::num;
return 0;
}
Upvotes: 1
Reputation: 483
Interesting question, what about this ( don't do that in production code ), lambdas can be declared without ()
be called thru operator ()
and never assigned:
#include <iostream>
int main() {
int num = 10;
[num_ = num] {
int num = 11;
std::cout << "num " << num << "\n";
std::cout << "outer_num " << num_ << "\n";
}();
}
Upvotes: 0
Reputation: 227
Probably it is something like a hacker task. I've spent some time dealing with pointers, etc. And got some solution:
#include <iostream>
int num = 0;
int main()
{
int *addressNumGlobal = #
std::cout << "Address num global: " << addressNumGlobal << endl;
int num = 1;
int *addressNum1 = #
std::cout << "Address of num 1: " << addressNum1 << std::endl;
{
int num = 2;
int *addressNum2 = #
std::cout << "Address of num 2: " << addressNum2 << std::endl;
{
int num = 3;
int *addressNum3 = #
std::cout << "Address of num 3: " << addressNum3 << std::endl;
char *myNum = (char*)(&num);
int *addressNum2 = (int*)(myNum + sizeof(int));
std::cout << "Calculated Address of num 2: " << addressNum2 << std::endl;
int num2Value = *addressNum2;
std::cout << num2Value << std::endl;
std::cout << ::num << std::endl;
}
}
return 0;
}
And I'm getting the following output:
Address num global: 0x601174
Address of num 1: 0x7ffc8db08b68
Address of num 2: 0x7ffc8db08b64
Address of num 3: 0x7ffc8db08b60
Calculated Address of num 2: 0x7ffc8db08b64
2
0
Of course, the addresses are changing from run to run but the access to num(2) is obtained:)
So num(1), num(2), num(3) are stored in a "row". And probably it will fail on a different compiler. But anyway it is a nice stuff to think of:)
Upvotes: -1
Reputation: 1460
You cannot just simply achieve this as the inner variable just overshadows your outer scope variable.
If an inner block declares a variable with the same name as the variable declared by the outer block, then the visibility of the outer block variable ends at the point of the declaration by inner block.
However, if you still need to achieve this, you can do something to store the variable values in a stack that saves the variable values per scope. This is something similar to what is done during function calls (just a revised version to store only variable you require).
I would still suggest you to instead use a separate name for variables as this reduces readability of code.
Upvotes: 7