Kev*_*ith 1 c++ private-members
// declare a class with private variable “int * _a”
// declare a function of this class, foo()
Void foo() {
int * _a; // 1. Does this re-declaration then make _a equal to an unknown value?
// 2. Does a go out of scope after foo() returns?
}
Run Code Online (Sandbox Code Playgroud)
- 这种重新声明是否
_a等于未知值?
不,这不是"重新申报".它是一个名为的局部变量的声明_a.它是未初始化的.
它与类成员变量没有任何关系_a.在声明本地之后_a,你不能_a再使用_a(因为_a引用局部变量!)来访问成员变量,但你可以使用它来引用它this->_a.
foo()退货后是否超出范围?
是.当声明它们的作用域结束时,局部变量超出范围(这就是"超出范围"的来源).