0% found this document useful (0 votes)
48 views

This Pointer

1) The 'this' pointer is passed implicitly as a hidden argument to non-static member functions and holds the address of the current object. 2) 'this' is used when a local variable name hides a member name, or to return a reference to the calling object allowing method chaining. 3) The dot (.) operator is used to access members of an object, while the arrow (->) operator is used to access members through a pointer to an object.

Uploaded by

sauravnaruka
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

This Pointer

1) The 'this' pointer is passed implicitly as a hidden argument to non-static member functions and holds the address of the current object. 2) 'this' is used when a local variable name hides a member name, or to return a reference to the calling object allowing method chaining. 3) The dot (.) operator is used to access members of an object, while the arrow (->) operator is used to access members through a pointer to an object.

Uploaded by

sauravnaruka
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

This pointer

this pointer in C++


The this pointer is passed as a hidden argument to all nonstatic member function calls and is available as a
local variable within the body of all nonstatic functions. this pointer is a constant pointer that holds the memory
address of the current object. this pointer is not available in static member functions as static member
functions can be called without any object (with class name).
For a class X, the type of this pointer is X* const. Also, if a member function of X is declared as const, then the
type of this pointer is const X *const (see this GFact)
Following are the situations where this pointer is used:
1) When local variables name is same as members name
#include<iostream>
using namespace std;
/* local variable is same as a member's name */
class Test
{
private:
int x;
public:
void setX (int x
{
// T!e 't!is' pointer is used to retrieve t!e ob"ect's x
// !idden b# t!e local variable 'x'
t!is$>x % x;
&
void print( { cout << 'x % ' << x << endl; &
&;
int main(
{
Test ob";
int x % ();
ob"*setX(x;
ob"*print(;
return );
&
Output:
x % ()
For constructors, initializer list can also be used when parameter name is same as members name.
2) To return reference to the calling object
/* +e,erence to t!e calling ob"ect can be returned */
Test- Test::,unc (
{
// .ome processing
return *t!is;
&
When a reference to a local object is returned, the returned reference can be used to chain function calls on a
single object.
#include<iostream>
using namespace std;
class Test
{
private:
int x;
int #;
public:
Test(int x % )/ int # % ) { t!is$>x % x; t!is$># % #; &
Test -setX(int a { x % a; return *t!is; &
Test -set0(int b { # % b; return *t!is; &
void print( { cout << 'x % ' << x << ' # % ' << # << endl; &
&;
int main(
{
Test ob"1(2/ 2;
// 3!ained ,unction calls* 4ll calls modi,# t!e same ob"ect
// as t!e same ob"ect is returned b# re,erence
ob"1*setX(1)*set0(();
ob"1*print(;
return );
&
Output:
x % 1) # % ()
. and -> arrow operator
C++ Member (dot & arrow) Operators
The . (dot) operator and the -> (arrow) operator are used to reference individual members of classes, structures, and
unions.
The dot operator is applied to the actual object. The arrow operator is used with a pointer to an object. For example,
consider the following structure:
struct 5mplo#ee {
c!ar ,irst6name7189;
int age;
& emp;
The (.) dot operator:
To assign the value "zara" to the frst_name member of object emp, you would write something as follows:
strcp#(emp*,irst6name/ ':ara';
The (->) arrow operator:
If p_emp is a pointer to an object of type Employee, then to assign the value "zara" to the frst_name member of
object emp, you would write something as follows:
strcp#(p6emp$>,irst6name/ ':ara';
The -> is called the arrow operator. It is formed by using the minus sign followed by a greater than sign.
Simply saying: To access members of a structure, use the dot operator. To access members of a structure through a
pointer, use the arrow operator.
Normally you can access data members and functions by using the dot (*) operator for 3at objects
created locally. To access the 3at object on the free store, you must dereference the pointer and call
the dot operator on the object pointed to by the pointer. Therefore, to access the ;et4ge member
function, you would write
(*p+ags*;et4ge(;
Parentheses are used to assure that p+ags is dereferenced before ;et4ge( is accessed.
Because this is cumbersome, C pro!ides a shorthand operator for indirect access" the points$to
operator ($>), which is created by typing the dash ($) immediately followed by the greater#than symbol
(>). C treats this as a single symbol. $isting %.& demonstrates accessing member !ariables and
functions of objects created on the free store.

You might also like