-6

so I saw this syntax

this->

used in a c++ tutorial on udemy but it was not explained to me as to what exactly why I would use this in programming. Can anyone provide an example program or proper example code that shows what this does? Please explain the logic behind it. Please don't say Google it. I know it has something to do with pointers and memory. and what not I guess another thing I am looking for is an explanation a five year old would understand or broken down enough for easy comprehension.

killer
  • 372
  • 1
  • 5
  • 15

1 Answers1

1

this keyword is used to reference current instance of given class, for example:

class A {
   public:

       void setName(std::string name) {
           // if you would use name variable directly it 
           // will refer to the function parameter, 
           //hence to refer the field of the class you need to use this
           this->name = name;
       }
   private:
       std::string namel
}
Artem Barger
  • 40,769
  • 9
  • 59
  • 81