C++ 11-作用域和全局变量
C++ 11- scopes & global variables
如何从内部范围访问全局变量,给定以下代码示例,如何从主函数和最内部范围访问全局字符串 X,也是最内部范围可访问一旦我们将它退出到主范围或其他范围?
#include <iostream>
#include <string>
std::string x = "global";
int counter = 1;
int main()
{
std::cout <<counter ++ << " " << x << std::endl;
std::string x = "main scope";
std::cout <<counter ++ << " " << x << std::endl;
{
std::cout <<counter ++ << " " << x << std::endl;
std::string x = "inner scope";
std::cout <<counter ++ << " " << x << std::endl;
}
std::cout <<counter++ << " " << x << std::endl;
}
当前 cout 是:
1 global
2 main scope
3 main scope
4 inner scope
5 main scope
可以使用 ::x
达到全局范围,根据:
#include <iostream>
#include <string>
std::string x = "global";
int counter = 1;
int main()
{
std::cout << counter++ << " " << x << std::endl;
std::string x = "main scope";
std::cout << " " << ::x << std::endl;
std::cout << counter++ << " " << x << std::endl;
{
std::cout << " " << ::x << std::endl;
std::cout << counter++ << " " << x << std::endl;
std::string x = "inner scope";
std::cout << " " << ::x << std::endl;
std::cout << counter++ << " " << x << std::endl;
}
std::cout << " " << ::x << std::endl;
std::cout << counter++ << " " << x << std::endl;
}
这给你:
1 global
global
2 main scope
global
3 main scope
global
4 inner scope
global
5 main scope
hard 位实际上进入中间范围,例如 main scope
当您使用内部范围时。
一种方法是使用引用:
#include <iostream>
#include <string>
std::string x = "outer";
int main()
{
std::cout << "1a " << x << "\n\n";
std::string x = "middle";
std::cout << "2a " << ::x << '\n';
std::cout << "2b " << x << "\n\n";
{
std::string &midx = x; // make ref to middle x.
std::string x = "inner"; // hides middle x.
std::cout << "3a " << ::x << '\n';
std::cout << "3b " << midx << '\n'; // get middle x via ref.
std::cout << "3c " << x << "\n\n";
}
}
给出:
1a outer
2a outer
2b middle
3a outer
3b middle
3c inner
但是,作为一个好建议,如果您:
,您会发现 附近 不会有那么多问题:
- 更智能地命名您的变量,以便避免冲突;和
- 避免像瘟疫一样的全局变量:-)
并且,对于内部作用域中的变量,一旦您离开该作用域,它们将不再可用,即使有一个引用(您可以将它们复制到一个变量中更大的范围,但这与访问内部范围变量不同)。
如何从内部范围访问全局变量,给定以下代码示例,如何从主函数和最内部范围访问全局字符串 X,也是最内部范围可访问一旦我们将它退出到主范围或其他范围?
#include <iostream>
#include <string>
std::string x = "global";
int counter = 1;
int main()
{
std::cout <<counter ++ << " " << x << std::endl;
std::string x = "main scope";
std::cout <<counter ++ << " " << x << std::endl;
{
std::cout <<counter ++ << " " << x << std::endl;
std::string x = "inner scope";
std::cout <<counter ++ << " " << x << std::endl;
}
std::cout <<counter++ << " " << x << std::endl;
}
当前 cout 是:
1 global
2 main scope
3 main scope
4 inner scope
5 main scope
可以使用 ::x
达到全局范围,根据:
#include <iostream>
#include <string>
std::string x = "global";
int counter = 1;
int main()
{
std::cout << counter++ << " " << x << std::endl;
std::string x = "main scope";
std::cout << " " << ::x << std::endl;
std::cout << counter++ << " " << x << std::endl;
{
std::cout << " " << ::x << std::endl;
std::cout << counter++ << " " << x << std::endl;
std::string x = "inner scope";
std::cout << " " << ::x << std::endl;
std::cout << counter++ << " " << x << std::endl;
}
std::cout << " " << ::x << std::endl;
std::cout << counter++ << " " << x << std::endl;
}
这给你:
1 global
global
2 main scope
global
3 main scope
global
4 inner scope
global
5 main scope
hard 位实际上进入中间范围,例如 main scope
当您使用内部范围时。
一种方法是使用引用:
#include <iostream>
#include <string>
std::string x = "outer";
int main()
{
std::cout << "1a " << x << "\n\n";
std::string x = "middle";
std::cout << "2a " << ::x << '\n';
std::cout << "2b " << x << "\n\n";
{
std::string &midx = x; // make ref to middle x.
std::string x = "inner"; // hides middle x.
std::cout << "3a " << ::x << '\n';
std::cout << "3b " << midx << '\n'; // get middle x via ref.
std::cout << "3c " << x << "\n\n";
}
}
给出:
1a outer
2a outer
2b middle
3a outer
3b middle
3c inner
但是,作为一个好建议,如果您:
,您会发现 附近 不会有那么多问题:- 更智能地命名您的变量,以便避免冲突;和
- 避免像瘟疫一样的全局变量:-)
并且,对于内部作用域中的变量,一旦您离开该作用域,它们将不再可用,即使有一个引用(您可以将它们复制到一个变量中更大的范围,但这与访问内部范围变量不同)。