C++ 变量作用域
一般来说有三个地方可以定义变量:
-
在函数或一个代码块内部声明的变量,称为局部变量。
-
在函数参数的定义中声明的变量,称为形式参数。
-
在所有函数外部声明的变量,称为全局变量。
作用域是程序的一个区域,变量的作用域可以分为以下几种:
-
局部作用域:在函数内部声明的变量具有局部作用域,它们只能在函数内部访问。局部变量在函数每次被调用时被创建,在函数执行完后被销毁。
-
全局作用域:在所有函数和代码块之外声明的变量具有全局作用域,它们可以被程序中的任何函数访问。全局变量在程序开始时被创建,在程序结束时被销毁。
-
块作用域:在代码块内部声明的变量具有块作用域,它们只能在代码块内部访问。块作用域变量在代码块每次被执行时被创建,在代码块执行完后被销毁。
-
类作用域:在类内部声明的变量具有类作用域,它们可以被类的所有成员函数访问。类作用域变量的生命周期与类的生命周期相同。
注意:如果在内部作用域中声明的变量与外部作用域中的变量同名,则内部作用域中的变量将覆盖外部作用域中的变量。
局部变量
在函数或一个代码块内部声明的变量,称为局部变量。它们只能被函数内部或者代码块内部的语句使用。下面的实例使用了局部变量:
实例
#include <iostream> using namespace std; int main () { // 局部变量声明 int a, b; int c; // 实际初始化 a = 10; b = 20; c = a + b; cout << c; return 0; }
全局变量
在所有函数外部定义的变量(通常是在程序的头部),称为全局变量。全局变量的值在程序的整个生命周期内都是有效的。
全局变量可以被任何函数访问。也就是说,全局变量一旦声明,在整个程序中都是可用的。下面的实例使用了全局变量和局部变量:
实例
https://avg.163.com/topic/detail/9158933 https://avg.163.com/topic/detail/9158928 https://avg.163.com/topic/detail/9158930 https://avg.163.com/topic/detail/9158931 https://avg.163.com/topic/detail/9158929 https://avg.163.com/topic/detail/9158932 https://avg.163.com/topic/detail/9158926 https://avg.163.com/topic/detail/9158925 https://avg.163.com/topic/detail/9158919 https://avg.163.com/topic/detail/9158917 https://avg.163.com/topic/detail/9158913 https://avg.163.com/topic/detail/9158918 https://avg.163.com/topic/detail/9158916 https://avg.163.com/topic/detail/9158914 https://avg.163.com/topic/detail/9158911 https://avg.163.com/topic/detail/9158912 https://avg.163.com/topic/detail/9158910 https://avg.163.com/topic/detail/9158902 https://avg.163.com/topic/detail/9158903 https://avg.163.com/topic/detail/9158901 https://avg.163.com/topic/detail/9158898 https://avg.163.com/topic/detail/9158899 https://avg.163.com/topic/detail/9158900 https://avg.163.com/topic/detail/9158896 https://avg.163.com/topic/detail/9158897 https://avg.163.com/topic/detail/9158895 https://avg.163.com/topic/detail/9158894 https://avg.163.com/topic/detail/9158883 https://avg.163.com/topic/detail/9158882 https://avg.163.com/topic/detail/9158881 https://avg.163.com/topic/detail/9158879 https://avg.163.com/topic/detail/9158876 https://avg.163.com/topic/detail/9158875 https://avg.163.com/topic/detail/9158870 https://avg.163.com/topic/detail/9158867 https://avg.163.com/topic/detail/9158853 https://avg.163.com/topic/detail/9158852 https://avg.163.com/topic/detail/9158851 https://avg.163.com/topic/detail/9158849 https://avg.163.com/topic/detail/9158846 https://avg.163.com/topic/detail/9158848 https://avg.163.com/topic/detail/9158845 https://avg.163.com/topic/detail/9158842 https://avg.163.com/topic/detail/9158824 https://avg.163.com/topic/detail/9158822 https://avg.163.com/topic/detail/9158820 https://avg.163.com/topic/detail/9158819 https://avg.163.com/topic/detail/9158816 https://avg.163.com/topic/detail/9158815 https://avg.163.com/topic/detail/9158814 https://avg.163.com/topic/detail/9158811 https://avg.163.com/topic/detail/9158809 https://avg.163.com/topic/detail/9158804 https://avg.163.com/topic/detail/9158803 https://avg.163.com/topic/detail/9158802 https://avg.163.com/topic/detail/9158801 https://avg.163.com/topic/detail/9158799 https://avg.163.com/topic/detail/9158798 https://avg.163.com/topic/detail/9158797 https://avg.163.com/topic/detail/9158795 https://avg.163.com/topic/detail/9158794 https://avg.163.com/topic/detail/9158793 https://avg.163.com/topic/detail/9158790 https://avg.163.com/topic/detail/9158789 https://avg.163.com/topic/detail/9158788 https://avg.163.com/topic/detail/9158786 https://avg.163.com/topic/detail/9158785 https://avg.163.com/topic/detail/9158784 https://avg.163.com/topic/detail/9158783 https://avg.163.com/topic/detail/9158782 https://avg.163.com/topic/detail/9158781 https://avg.163.com/topic/detail/9158778 https://avg.163.com/topic/detail/9158777 https://avg.163.com/topic/detail/9158776 https://avg.163.com/topic/detail/9158775 https://avg.163.com/topic/detail/9158774 https://avg.163.com/topic/detail/9158773 https://avg.163.com/topic/detail/9158771 https://avg.163.com/topic/detail/9158770 https://avg.163.com/topic/detail/9158769 https://avg.163.com/topic/detail/9158766 https://avg.163.com/topic/detail/9158765 https://avg.163.com/topic/detail/9158764 https://avg.163.com/topic/detail/9158763 https://avg.163.com/topic/detail/9158762 https://avg.163.com/topic/detail/9158760 https://avg.163.com/topic/detail/9158758 https://avg.163.com/topic/detail/9158759 https://avg.163.com/topic/detail/9158757 https://avg.163.com/topic/detail/9158756 https://avg.163.com/topic/detail/9158742 https://avg.163.com/topic/detail/9158740 https://avg.163.com/topic/detail/9158738 https://avg.163.com/topic/detail/9158736 https://avg.163.com/topic/detail/9158735 https://avg.163.com/topic/detail/9158733 https://avg.163.com/topic/detail/9158734 https://avg.163.com/topic/detail/9158731 https://avg.163.com/topic/detail/9158728 https://avg.163.com/topic/detail/9158715 https://avg.163.com/topic/detail/9158714 https://avg.163.com/topic/detail/9158713 https://avg.163.com/topic/detail/9158712 https://avg.163.com/topic/detail/9158711 https://avg.163.com/topic/detail/9158710 https://avg.163.com/topic/detail/9158708 https://avg.163.com/topic/detail/9158707 https://avg.163.com/topic/detail/9158706 https://avg.163.com/topic/detail/9158704 https://avg.163.com/topic/detail/9158688 https://avg.163.com/topic/detail/9158686 https://avg.163.com/topic/detail/9158682 https://avg.163.com/topic/detail/9158678 https://avg.163.com/topic/detail/9158676 https://avg.163.com/topic/detail/9158675 https://avg.163.com/topic/detail/9158674 https://avg.163.com/topic/detail/9158672 https://avg.163.com/topic/detail/9158658 https://avg.163.com/topic/detail/9158657 https://avg.163.com/topic/detail/9158656 https://avg.163.com/topic/detail/9158655 https://avg.163.com/topic/detail/9158652 https://avg.163.com/topic/detail/9158654 https://avg.163.com/topic/detail/9158651 https://avg.163.com/topic/detail/9158624 https://avg.163.com/topic/detail/9158621 https://avg.163.com/topic/detail/9158618 https://avg.163.com/topic/detail/9158616 https://avg.163.com/topic/detail/9158622 https://avg.163.com/topic/detail/9158620 https://avg.163.com/topic/detail/9158617 https://avg.163.com/topic/detail/9158605 https://avg.163.com/topic/detail/9158603 https://avg.163.com/topic/detail/9158601 https://avg.163.com/topic/detail/9158599 https://avg.163.com/topic/detail/9158598 https://avg.163.com/topic/detail/9158596 https://avg.163.com/topic/detail/9158597 https://avg.163.com/topic/detail/9158594 https://avg.163.com/topic/detail/9158593 https://avg.163.com/topic/detail/9158592 https://avg.163.com/topic/detail/9158567 https://avg.163.com/topic/detail/9158566 https://avg.163.com/topic/detail/9158524 https://avg.163.com/topic/detail/9158564 https://avg.163.com/topic/detail/9158562 https://avg.163.com/topic/detail/9158563 https://avg.163.com/topic/detail/9158561 https://avg.163.com/topic/detail/9158560 https://avg.163.com/topic/detail/9158559 https://avg.163.com/topic/detail/9158558 https://avg.163.com/topic/detail/9158534 https://avg.163.com/topic/detail/9158527 https://avg.163.com/topic/detail/9158524 https://avg.163.com/topic/detail/9158521 https://avg.163.com/topic/detail/9158520 https://avg.163.com/topic/detail/9158516 https://avg.163.com/topic/detail/9158517 https://avg.163.com/topic/detail/9158515 https://avg.163.com/topic/detail/9158510 https://avg.163.com/topic/detail/9158462 https://avg.163.com/topic/detail/9158457 https://avg.163.com/topic/detail/9158454 https://avg.163.com/topic/detail/9158453 https://avg.163.com/topic/detail/9158450 https://avg.163.com/topic/detail/9158447 https://avg.163.com/topic/detail/9158446 https://avg.163.com/topic/detail/9158444 https://avg.163.com/topic/detail/9158398 https://avg.163.com/topic/detail/9158391 https://avg.163.com/topic/detail/9158390 https://avg.163.com/topic/detail/9158388 https://avg.163.com/topic/detail/9158386 https://avg.163.com/topic/detail/9158381 https://avg.163.com/topic/detail/9158382 https://avg.163.com/topic/detail/9158380 https://avg.163.com/topic/detail/9158379 https://avg.163.com/topic/detail/9158375 https://avg.163.com/topic/detail/9158316 https://avg.163.com/topic/detail/9158312 https://avg.163.com/topic/detail/9158309 https://avg.163.com/topic/detail/9158308 https://avg.163.com/topic/detail/9158303 https://avg.163.com/topic/detail/9158305 https://avg.163.com/topic/detail/9158306 https://avg.163.com/topic/detail/9158302 https://avg.163.com/topic/detail/9158299 https://avg.163.com/topic/detail/9158245 https://avg.163.com/topic/detail/9158237 https://avg.163.com/topic/detail/9158234 https://avg.163.com/topic/detail/9158232 https://avg.163.com/topic/detail/9158230 https://avg.163.com/topic/detail/9158225 https://avg.163.com/topic/detail/9158229 https://avg.163.com/topic/detail/9158227 https://avg.163.com/topic/detail/9158228 https://avg.163.com/topic/detail/9158221 https://avg.163.com/topic/detail/9158167 https://avg.163.com/topic/detail/9158162 https://avg.163.com/topic/detail/9158161 https://avg.163.com/topic/detail/9158159 https://avg.163.com/topic/detail/9158160 https://avg.163.com/topic/detail/9158163 https://avg.163.com/topic/detail/9158157 https://avg.163.com/topic/detail/9158086 https://avg.163.com/topic/detail/9158081 https://avg.163.com/topic/detail/9158083 https://avg.163.com/topic/detail/9158078 https://avg.163.com/topic/detail/9158075 https://avg.163.com/topic/detail/9158079 https://avg.163.com/topic/detail/9158076 https://avg.163.com/topic/detail/9158074 https://avg.163.com/topic/detail/9158072 https://avg.163.com/topic/detail/9158018 https://avg.163.com/topic/detail/9158011 https://avg.163.com/topic/detail/9158012 https://avg.163.com/topic/detail/9158007 https://avg.163.com/topic/detail/9158009 https://avg.163.com/topic/detail/9158010 https://avg.163.com/topic/detail/9158008 https://avg.163.com/topic/detail/9158006 https://avg.163.com/topic/detail/9158004 https://avg.163.com/topic/detail/9158005 https://avg.163.com/topic/detail/9157916 https://avg.163.com/topic/detail/9157915 https://avg.163.com/topic/detail/9157913 https://avg.163.com/topic/detail/9157918 https://avg.163.com/topic/detail/9157914 https://avg.163.com/topic/detail/9157912 https://avg.163.com/topic/detail/9157910 https://avg.163.com/topic/detail/9157909 https://avg.163.com/topic/detail/9157908 https://avg.163.com/topic/detail/9157853 https://avg.163.com/topic/detail/9157847 https://avg.163.com/topic/detail/9157591 https://avg.163.com/topic/detail/9157745 https://avg.163.com/topic/detail/9157756 https://avg.163.com/topic/detail/9157845 https://avg.163.com/topic/detail/9157843 https://avg.163.com/topic/detail/9157846 https://avg.163.com/topic/detail/9157844 https://avg.163.com/topic/detail/9157842 https://avg.163.com/topic/detail/9157756 https://avg.163.com/topic/detail/9157755 https://avg.163.com/topic/detail/9157749 https://avg.163.com/topic/detail/9157748 https://avg.163.com/topic/detail/9157747 https://avg.163.com/topic/detail/9157745 https://avg.163.com/topic/detail/9157744 https://avg.163.com/topic/detail/9157683 https://avg.163.com/topic/detail/9157680 https://avg.163.com/topic/detail/9157678 https://avg.163.com/topic/detail/9157676 https://avg.163.com/topic/detail/9157675 https://avg.163.com/topic/detail/9157673 https://avg.163.com/topic/detail/9157592 https://avg.163.com/topic/detail/9157593 https://avg.163.com/topic/detail/9157589 https://avg.163.com/topic/detail/9157587 https://avg.163.com/topic/detail/9157591 https://avg.163.com/topic/detail/9157588 https://avg.163.com/topic/detail/9157586 https://avg.163.com/topic/detail/9157585 https://avg.163.com/topic/detail/9157584 https://avg.163.com/topic/detail/9157583 https://avg.163.com/topic/detail/9157432 https://avg.163.com/topic/detail/9157199
#include <iostream> using namespace std; // 全局变量声明 int g; int main () { // 局部变量声明 int a, b; // 实际初始化 a = 10; b = 20; g = a + b; cout << g; return 0; }
在程序中,局部变量和全局变量的名称可以相同,但是在函数内,局部变量的值会覆盖全局变量的值。下面是一个实例:
实例
#include <iostream> using namespace std; // 全局变量声明 int g = 20; int main () { // 局部变量声明 int g = 10; cout << g; return 0; }
当上面的代码被编译和执行时,它会产生下列结果:
10
初始化局部变量和全局变量
当局部变量被定义时,系统不会对其初始化,您必须自行对其初始化。定义全局变量时,系统会自动初始化为下列值:
| int | 0 |
| char | '\\0' |
| float | 0 |
| double | 0 |
| pointer | NULL |
正确地初始化变量是一个良好的编程习惯,否则有时候程序可能会产生意想不到的结果。
块作用域指的是在代码块内部声明的变量:
实例
#include <iostream> int main() { int a = 10; { int a = 20; // 块作用域变量 std::cout << "块变量: " << a << std::endl; } std::cout << "外部变量: " << a << std::endl; return 0; }
以上实例中,内部的代码块中声明了一个名为 a 的变量,它与外部作用域中的变量 a 同名。内部作用域中的变量 a 将覆盖外部作用域中的变量 a,在内部作用域中访问 a 时输出的是20,而在外部作用域中访问 a 时输出的是 10。
当上面的代码被编译和执行时,它会产生下列结果:
块变量: 20
外部变量: 10
类作用域
类作用域指的是在类内部声明的变量:
实例
#include <iostream> class MyClass { public: static int class_var; // 类作用域变量 }; int MyClass::class_var = 30; int main() { std::cout << "类变量: " << MyClass::class_var << std::endl; return 0; }
以上实例中,MyClass 类中声明了一个名为 class_var 的类作用域变量。可以使用类名和作用域解析运算符 :: 来访问这个变量。在 main() 函数中访问 class_var 时输出的是 30。
网硕互联帮助中心







评论前必须登录!
注册