C++ this Pointer
In C++,this The this pointer is a special pointer that points to the instance of the current object.
In C++, every object can access its own address through this the this pointer.
thisthe this pointer is a hidden pointer that can be used in a class's member functions to point to the calling object.
When a member function of an object is called, the compiler implicitly passes the object's address as the this pointer.
Friend functions do not have this the this pointer, because a friend is not a member of the class; only member functions have this pointer.
The following example helps to better understand the concept of the this pointer:
Example
#include
<
iostream
class
MyClass
{
private:
int
value
;
public
:
void
setValue
(
int
value
)
{
this
->
value
=
value
;
}
void
printValue
(
)
{
std
::
cout
<<
"
Value:
"
<<
this
->
value
<<
std
::
endl
;
}
}
;
int
main
(
)
{
MyClass
obj
;
obj
.
setValue
(
42
)
;
obj
.
printValue
(
)
;
return
0
;
}
The output of the above code is:
Value: 42
Example analysis:
-
In the above example, we define a class named MyClass that has a private member variable value.
-
The setValue() function in the class is used to set the value of value, while the printValue() function is used to print the value of value.
-
In the setValue() function, we use the this pointer to refer to the member variable value of the current object and assign the passed-in value to it, making it clear to the compiler that we want to access the current object's member variable rather than a function parameter or a local variable.
-
In the printValue() function, we likewise use the this pointer to refer to the current object's member variable value and print it out.
-
In the main() function, we create an object obj of MyClass, then use the setValue() function to set the value of value to 42, and print it out via the printValue() function.
-
By using the this pointer, we can access the current object's member variables in member functions even when they share the same name as function parameters or local variables, avoiding naming conflicts and ensuring we access the correct variable.
More examples
The following example compares the volumes of cuboids:
Example
#include
<
iostream
using
namespace
std;
class
Box
{
public
:
// Constructor definition
Box
(
double
l
=
2
.0
,
double
b
=
2
.0
,
double
h
=
2
.0
)
{
cout
<<
"
Constructor called.
"
<<
endl
;
length
=
l
;
breadth
=
b
;
height
=
h
;
}
double
Volume
(
)
{
return
length
breadth
height
;
}
int
compare
(
Box
box
)
{
return
this
->
Volume
(
)
box
.
Volume
(
)
;
}
private
:
double
length
;
// width
double
breadth
;
// length
double
height
;
// height
}
;
int
main
(
void
)
{
Box
Box1
(
3
.3
,
1
.2
,
1
.5
)
;
// Declare box1
Box
Box2
(
8
.5
,
6
.0
,
2
.0
)
;
// Declare box2
if
(
Box1
.
compare
(
Box2
)
)
{
cout
<<
"
The volume of Box2 is smaller than that of Box1
"
<<
endl
;
}
else
{
cout
<<
"
The volume of Box2 is greater than or equal to that of Box1
"
<<
endl
;
}
return
0
;
}
When the code above is compiled and executed, it produces the following result:
调用构造函数。
调用构造函数。
Box2 的体积大于或等于 Box1
C++ Classes & Objects