Anda di halaman 1dari 7

 Topic : Introduction To "This" Pointer | Pointer

C & C++ Classes in


Chandigarh
Find India's Best C & C++
Classes Search by Area,
Experience & Rates!

Ad urbanpro.com Visit Site

POINTER TO OBJECTS:
A Variable That Holds an Address value is called a Pointer
variable or simply pointer. We already discussed about
pointer that's point to Simple data types likes int, char,
float etc. So similar to these type of data type, Objects
can also have an address, so there is also a pointer that
can point to the address of an Object, This Pointer is
Known as This Pointer.

THIS POINTER:
Every Object in C++ has access to its own address
through an important pointer called T h i s Pointer. The
This Pointer is an implicit parameter to all member
functions. Therefore, inside a member function, this may
be used to refer to the invoking object. Whenever a
member function is called, it is automatically passed an
implicit arguments that is This pointer to the invoking
object (i.e. The object on which the function is invoked).
?
 T h iTopic
The s pointer:is Introduction
passed as aTo "This"argument
hidden Pointer | Pointer
to all
Nonstatic member function calls and is available as a
local variable within the body of all Nonstatic functions.
T h i s Pointer is a constant pointer that holds the memory
address of the current object. T h i s pointer is not
available in static member functions as static member
functions can be called without any object (with class
name).

Let's have an simple example of This pointer.

/* This Pointer: InfoBrother */


 
#include<iostream>
using namespace std;
 
class Point
{
private:
int x;
 

In Above Example, there is an simple class having two


objects, and a simple function that take an integer as an
parameter, and initialize it to variable x. Now in above
Example, Might be you have a Question, That

How Setx() function understand which object 'x' value to


be set?

So the Answer is, When the function is called the address


of the calling object is passed implicitly. so in order to
understand, consider the following statement.

obj1.setx( 10 );

Here value 10 is passed explicitly to the function setx()


but at the same time Address of o b j 1 is passed implicitly
to the function. " Int a" holds the value 10, and the address
of obj1 stored in a pointer named " this " pointer.
 Topic
Similarly when o b : calls the function
j 2 Introduction of the
To "This" class,| Pointer
Pointer " This
" pointer holds address of obj2 to refer obj2's data
member. at shown in figure:

C++ DECLARATION AND USES OF OBJECT POINTERS:


Just like Other Pointers, the object pointers are declared
by placing in front of a object pointer's name. The Syntax
is:

class_name * Object_pointer_name;

In above Syntax, class_Name is the name of an already


defined class and object_pointer_name is the pointer to
an object of this class type. For example: To declared an
Pointer p t r an an object pointer of class D a t e , We shall
write:

Date * ptr;

In Above syntax, Date is already defined class. When


accessing members of a class using an object pointer, the
Error Operator - > is used instead of Dot Operator. Let's
take an simple example, This Example illustrates how to
access an object given a pointer to it. The following
access a object g e a po te to t. e o o g
Example illustrates the use of object pointer.
 Topic : Introduction To "This" Pointer | Pointer

/* Pointer to Object: This Pointer: InfoBr


 
#include<iostream>
using namespace std;
 
class Date
{
private:
short int dd, mm, yy;
 

Check Output

The above Example is self-explanatory. To Access public


members using an object the Dot ( . ) Operator is used,
and to access public members using an Object pointer,
the Arrow ( -> )operator is used.

« Previous      Next Topic »

“ I Tried my Best to Provide


you complete Information regarding
this topic in very easy and conceptual
way. but still if you have any Problem
to understand this topic, or do you
have any Questions, Feel Free to Ask
Question. i'll do my best to Provide
you what you need.

Do You have any Question?

Sardar Omar.
InfoBrother
 Topic : Introduction To "This" Pointer | Pointer

 Write For InfoBrother

ADVERTISING

C & C++ Classes in


Chandigarh
Best Institutes in Chandigarh
Find India's Best C & C++ Classes Search by
Area, Experience & Rates!
urbanpro.com

OPEN
 Get:More
Topic Tutorials To
Introduction for"This"
Free: Pointer | Pointer

Read some Interesting Articles:

ADVERTISEMENT

Info Brother is optimized for learning, testing and training. An


example might be simplified to improve reading and basic
understanding. Tutorial, references, and examples are
constantly reviewed to avoid errors, but we can't warrant full
correctness of all content. while using this site, you agree to
have read and accepted our terms of use, cookie and privacy
policy.

     
 Meet Our Team  Privacy Policy Term of Use
 Topic : Introduction To "This" Pointer | Pointer
 Write For Us  FAQ  Help Us  Contact Us

Copyright © 2015 - 2019 - Info Brother, all rights reserved.

Anda mungkin juga menyukai