What is the scope of variables declared in a class constructor?(类构造函数中声明的变量的范围是什么?)
问题描述
我很好奇,在类构造函数中声明的不是该类的数据成员的变量的范围是什么?
I was curious, what is the scope of variables declared inside a class constructor which are not data members of that class?
例如,如果一个构造函数需要一个迭代 int i
,这个变量会在构造函数完成后被销毁,还是对程序来说是全局的?
For example, if a constructor needs an iterating int i
, will this variable be destroyed after the constructor finishes, or is it then global for the program?
推荐答案
在这个意义上,构造函数就像任何其他函数一样 - 在内部声明的任何变量都有通常的作用域限制,一旦构造函数被破坏,它们肯定会超出作用域并被销毁完成.
In this sense a constructor is like any other function - any variable declared inside has usual scope limitations and they all surely go out of scope and get destroyed once constructor is finished.
这篇关于类构造函数中声明的变量的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:类构造函数中声明的变量的范围是什么?


基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01