简体   繁体   English

成员函数中声明的变量与成员变量相同

[英]variable declared in member function is same as member variable

wondering whether this gonna work, and how: 想知道这是否行得通,以及如何:

class sample
{
 int i;
 int func1()
  {
   int i = 0;
   i++;
   return i;
  }
}

reason I ask is because I have many member functions and bad name conventions. 我问的原因是因为我有很多成员函数和错误的名称约定。

When you say int i = 0 you're creating a new variable called i that hides the class member. 当您说int i = 0您正在创建一个名为i的新变量,该变量将隐藏类成员。 If you want to access the class's i , you can do this->i . 如果要访问类的i ,则可以执行this->i But it's usually better not to cause that kind of confusion in the first place. 但是通常最好不要一开始就造成这种混乱。

Inside the body of func1 , you will be referencing the locally declared int i . func1的主体内,您将引用本地声明的int i In order to reference the class member, you need to reference it explicitly by using the this pointer: 为了引用该类成员,您需要使用this指针显式引用它:

this->i

this is a const pointer passed in to all methods in a class to represent the current instance. this是一个const指针, this指针传递给类中的所有方法以表示当前实例。 It is not passed in when you have a static member function of course. 当然,当您具有static成员函数时,不会传递它。

The reason the locally declared int i is being used first is because it is in the same scope as i++ and return i . 首先使用本地声明的int i的原因是因为它与i++处于同一范围,并return i

Variables inside func1 refer to int i = 0; func1内部的变量引用int i = 0; (nearest i ). (最接近i )。

In C++, identical name variables will be used from same/nearset scope first. 在C ++中,将首先从相同/相邻范围中使用相同的名称变量。

It works fine. 工作正常。 All uses of the name i inside the function refer to the i declared inside that function. 函数内部对名称i所有使用均指该函数内部声明的i That is, the function will return 1 every time. 也就是说,该函数每次都会返回1。

What is your intention of i inside the func1(). 您对func1()中的i的意图是什么。 Do you want to increment the outside i or the i inside the function. 是否要增加外部i或函数内部的i。 If you want the outside i to increment then this won't work. 如果您希望外部i增加,则将无法使用。

Things get weird with scopes: 范围变得奇怪了:

int func1()
{
    int i = 0;
    i++;
    { //1
        int i = 41;
        i++;
    }
    { //2
        int j = i + 1;
        cout << j << endl // this prints 2
    }
    return i;
}

The rule when using variables in scope is, it always refers to the most local scope first and works it way up. 在范围内使用变量的规则是,它始终总是首先引用最局部的范围并逐步向上使用。 So in your example the i inside your function will not refer to the i in the class. 因此,在您的示例中,函数内的i将不会引用类中的i

The return will in fact refer to the i declared in func1() . 实际上,该返回将引用在func1()声明的i It's all about scopes. 这都是关于范围的。

A scope starts with { and end with } . 范围以{开头,以}结尾。 All variables declared inside a scope will only be defined as long you stay within the scope or if you go into another scope. 仅当您在范围内或进入另一个范围时,才定义在范围内声明的所有变量。 Hence 因此

{ int i = 0; { int i = 1; { int i = 2; }}}

is perfectly possible. 完全有可能。 If you use i in one of the scopes you will always refer to the i in the same scope. 如果您在范围之一中使用i ,则将始终在同一范围中引用i To refer to an i of a higher scope is more difficult. 引用范围更大的i更加困难。

In your example you can still refer to the top i by using this->i , where this is a pointer to the object you are working with. 在您的示例中,您仍然可以使用this->i来引用顶部ithis是您正在使用的对象的指针。 Here is some more info (scroll a bit down). 是一些更多信息(向下滚动)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM