What is purpose of a “this” pointer in C++? [dupli

2020-05-25 07:53发布

What is purpose of this keyword. Doesn't the methods in a class have access to other peer members in the same class ? What is the need to call a this to call peer methods inside a class?

17条回答
混吃等死
2楼-- · 2020-05-25 08:24

The this pointer is a way to access the current instance of particular object. It can be used for several purposes:

  • as instance identity representation (for example in comparison to other instances)
  • for data members vs. local variables disambiguation
  • to pass the current instance to external objects
  • to cast the current instance to different type
查看更多
贪生不怕死
3楼-- · 2020-05-25 08:25

It also allows you to test for self assignment in assignment operator overloads:

Object & operator=(const Object & rhs) {
  if (&rhs != this) {
    // do assignment
  }
  return *this;
}
查看更多
再贱就再见
4楼-- · 2020-05-25 08:26

One more purpose is to chaining object: Consider the following class:

class Calc{
private:
    int m_value;

public:
    Calc() { m_value = 0; }

    void add(int value) { m_value += value; }
    void sub(int value) { m_value -= value; }
    void mult(int value) { m_value *= value; }

    int getValue() { return m_value; }
};

If you wanted to add 5, subtract 3, and multiply by 4, you’d have to do this: #include int main() { Calc calc; calc.add(5); // returns void calc.sub(3); // returns void calc.mult(4); // returns void

    std::cout << calc.getValue() << '\n';
    return 0;
}

However, if we make each function return *this, we can chain the calls together. Here is the new version of Calc with “chainable” functions:

class Calc
{
private:
    int m_value;

public:
    Calc() { m_value = 0; }

    Calc& add(int value) { m_value += value; return *this; }
    Calc& sub(int value) { m_value -= value; return *this; }
    Calc& mult(int value) { m_value *= value; return *this; }

    int getValue() { return m_value; }
};

Note that add(), sub() and mult() are now returning *this. Consequently, this allows us to do the following:

#include <iostream>
int main()
{
    Calc calc;
    calc.add(5).sub(3).mult(4);

    std::cout << calc.getValue() << '\n';
    return 0;
}

We have effectively condensed three lines into one expression.

Copied from :http://www.learncpp.com/cpp-tutorial/8-8-the-hidden-this-pointer/

查看更多
干净又极端
5楼-- · 2020-05-25 08:29

It allows you to get around members being shadowed by method arguments or local variables.

查看更多
Explosion°爆炸
6楼-- · 2020-05-25 08:29

For example if you write an operator=() you must check for self assignment.

class C {
public:
    const C& operator=(const C& rhs)
    {
        if(this==&rhs) // <-- check for self assignment before anything
            return *this;
        // algorithm of assignment here
        return *this; // <- return a reference to yourself
    }
};
查看更多
Animai°情兽
7楼-- · 2020-05-25 08:29

The double colon in c++ is technically known as "Unary Scope resolution operator". Basically it is used when we have the same variable repeated for example inside our "main" function (where our variable will be called local variable) and outside main (where the variable is called a global variable). C++ will alwaysexecute the inner variable ( that is the local one). So imagine you want to use the global variable "Conundrum" instead the local one just because the global one is expressed as a float instead of as an integer:

#include <iostream>
using namespace std;

float Conundrum=.75;

int main()
{
  int Conundrum =75;
  cout<<::Conundrum;

}

So in this case the program will use our float Conundrum instead of the int Conundrum.

查看更多
登录 后发表回答