类构造函数中声明的变量的范围是什么?

2021-12-30 00:00:00 constructor scope c++

我很好奇,在类构造函数中声明的不是该类的数据成员的变量的范围是什么?

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.

相关文章