In C++, this pointer is a pointer that points to the object on which a member function is called. It allows the member function to access and modify the object's data members.
Here's a concise example of this pointer in C++:
#include <iostream>
class MyClass
{
public:
int data;
MyClass(int data)
{
this->data = data; // 'this' is used to access the member variable
}
void display()
{
std::cout << "Data: " << this->data << std::endl;
}
};
int main()
{
MyClass object(2);
object.display();
return 0;
}
It looks like you're new here. If you want to get involved, click one of these buttons!