Sunteți pe pagina 1din 10

POINTER AND USES OF THIS POINTER

INTRODUCTION

C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily
with pointers, and other C++ tasks, such as dynamic memory allocation, cannot be performed
without them. As you know every variable is a memory location and every memory location
has its address defined which can be accessed using ampersand (&) operator which denotes
an address in memory.

POINTER
A pointer is a variable whose value is the address of another variable. Like any
variable or constant, you must declare a pointer before you can work with it. The general
form of a pointer variable declaration is:

type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the
name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk
that you use for multiplication. However, in this statement the asterisk is being used to
designate a variable as a pointer. Following are the valid pointer declaration:

int *ip; // pointer to an integer


double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory address. The
only difference between pointers of different data types is the data type of the variable or
constant that the pointer points to. There are few important operations, which we will do with
the pointers very frequently. (a) we define a pointer variables (b) assign the address of a
variable to a pointer and (c) finally access the value at the address available in the pointer
variable. This is done by using unary operator * that returns the value of the variable located
at the address specified by its operand.

1
A pointer is a variable that holds a memory address. This address is the location of
another object (typically, a variable) in memory. That is, if one variable contains the address
of another variable, the first variable is said to point to the second.
A pointer declaration consists of a base type, an *, and the variable name.

The general form of declaring a pointer variable is :

type *name;

The 'type' is the base type of the pointer and may be any valid type. The 'name' is the
name of pointer variable. The base type of the pointer defines what type of variables the
pointer can point to.

SPECIAL POINTER OPERATORS

There are two special pointer operators.

They are * and &.

The & is unary operator that returns the memory address of its operand. It is the
address of operand. The * is complement of &. It is also a unary operator and returns the
value located at the address that follows.

int i, *p;

i = 5;

p = &i; //places the memory address of i into p

The expression *p will return the value of variable pointed to by p.

2
AIM

To illustrate the usage of pointer

ALGORITHM

1. Start the program

2. Declare actual variable named var as 20

3. Declare pointer variable named *ip and actual variable named var

4. Print the value of var, ip and *ip.

SOURCE CODE

#include <iostream>

using namespace std;

int main ()
{
int var = 20; // actual variable declaration.
int *ip; // pointer variable

ip = &var; // store address of var in pointer variable

cout << "Value of var variable: ";


cout << var << endl;

// print the address stored in ip pointer variable


cout << "Address stored in ip variable: ";
cout << ip << endl;

// access the value at the address available in pointer


cout << "Value of *ip variable: ";

3
cout << *ip << endl;

return 0;
}
OUTPUT
When the above code is compiled and executed, it produces result something as follows:

Value of var variable: 20


Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20

C++ POINTERS IN DETAIL


Pointers have many but easy concepts and they are very important to C++
programming. There are following few important pointer concepts which should be clear to a
C++ programmer:

CONCEPT DESCRIPTION
C++ supports null pointer, which is a constant with a
C++ Null Pointers
value of zero defined in several standard libraries.
There are four arithmetic operators that can be used
C++ pointer arithmetic
on pointers: ++, --, +, -
There is a close relationship between pointers and
C++ pointers vs arrays
arrays. Let us check how?
You can define arrays to hold a number of pointers.
C++ array of pointers
C++ allows you to have pointer on a pointer and so
C++ pointer to pointer
on.
Passing an argument by reference or by address both
Passing pointers to functions
enable the passed argument to be changed in the
calling function by the called function.
C++ allows a function to return a pointer to local
Return pointer from functions
variable, static variable and dynamically allocated
memory as well.

POINTER TO CONSTANT VS. POINTER CONSTANT

Pointer to constant points to a value that does not change and is declared as :

4
const type * name

type is data type

name is name of the pointer

EXAMPLE

const char *p;

Pointer to constant can not be used to change the value being pointed to. Therefore :

char ch = A;

const char *p = &ch;

*p = B; is not allowed. The program will throw an error.

POINTER CONSTANT (OR CONSTANT POINTER)

It is a pointer which you dont want to be pointed to a different value. That is, the
location stored in the pointer can not change. We can not change where the pointer points. It
is declared as :

type * const name

type is data type

name is name of the pointer

EXAMPLE

char * const p

Since the location to which a const pointer points to can not be changed, the following
code :

char ch1 = A;

5
char ch2 = B;

char * const p = &ch1;

p = &ch2; will throw an error since address stored in p can not be changed

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)

USE OF THIS POINTER

The this keyword is used to represent an object that invokes the member function. It
points to the object for which this function was called. It is automatically passed to a member
function when it is called. For example when you call A.func(), this will be set to the address
of A. When a member function is called, it is automatically passed an implicit argument that
is a pointer to the invoking object (i.e. the object on which the function is invoked).

This pointer is known as 'this' pointer. It is internally created at the time of function
call. The this pointer is very important when operators are overloaded. Every object in C++
has access to its own address through an important pointer called this 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. Friend functions do not have a this
pointer, because friends are not members of a class. Only member functions have a this
pointer.

AIM

To illustrate the usage of this pointer

6
ALGORITHM

1. Start the program

2. Declare integer and float variable named i and f respectively

3. Using overloaded operator function return the value of pointer to function named
show.

4. Print the values of i and f inside show function

EXAMPLE

Consider a class with int and float data members and overloaded Pre-increment
operator ++.

SOURCE CODE

class MyClass

{
int i;

float f;

public:
MyClass ()

{
i = 0;

f = 0.0;

}
MyClass (int x, float y)

{
i = x; f = y;

7
}
MyClass operator ++()

{
i = i + 1;

f = f + 1.0;

return *this; //this pointer which points to the caller object

}
MyClass show()

{
cout<<The elements are:\n cout<i<<\n<f; //accessing data members using this

}
};

int main()

{
MyClass a;

++a; //The overloaded operator function ++()will return as this pointer


a.show();
retun 0;

OUTPUT

The output would be:

The elements are:

1
1.0

8
SITUATION TO USE THIS POINTER

The 'this pointer' is used as a pointer to the class object instance by the member
function. The address of the class instance is passed as an implicit parameter to the member
functions. The keyword this is a prvalue expression whose value is the address of the object,
on which the member function is being called. It can appear in the following contexts:

Within the body of any non-static member function, including member initializer
list
Within the declaration of a non-static member function anywhere after the
(optional) cv-qualifier sequence, including dynamic exception
specification(deprecated), noexcept specification, and the trailing return type
Within default member initializer

The type of this in a member function of class X is X* (pointer to X). If the member
function is cv-qualified, the type of this is cv X* (pointer to identically cv-qualified X). Since
constructors and destructors cannot be cv-qualified, the type of this in them is always X*,
even when constructing or destroying a const object. When a non-static class member is used
in any of the contexts where the this keyword is allowed (non-static member function bodies,
member initializer lists, default member initializers), the implicit this-> is automatically
added before the name, resulting in a member access expression (which, if the member is a
virtual member function, results in a virtual function call).

In class templates, this is a dependent expression, and explicit this-> may be used to
force another expression to become dependent. It is possible to execute delete this;, if the
program can guarantee that the object was allocated by new, however, this renders every
pointer to the deallocated object invalid, including the this pointer itself: after delete this;
returns, such member function cannot refer to a member of a class (since this involves an
implicit dereference of this) and no other member function may be called. This is used, for
example, in the member function of the control block of std::shared_ptr responsible for
decrementing the reference count, when the last reference to the managed object goes out of
scope.

CONCLUSION

9
C++ has added a new parameter to the function. The added parameter is a pointer to
the class object the class function is working with. It is always named this. The this pointer is
a hidden pointer inside every class member function that points to the class object. As a
result, an object's this pointer is not part of the object itself; it is not reflected in the result of a
sizeof statement on the object. Instead, when a nonstatic member function is called for an
object, the address of the object is passed by the compiler as a hidden argument to the
function.

REFERENCES

http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm
http://www.geeksforgeeks.org/this-pointer-in-c/
https://msdn.microsoft.com/en-us/library/y0dddwwd.aspx
http://www.bogotobogo.com/cplusplus/this_pointer.php
http://stackoverflow.com/questions/16492736/what-is-the-this-pointer
http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm
http://www.dailyfreecode.com/code/provides-example-pointer-2327.aspx
http://www.careerride.com/C++-use-of-this-pointer.aspx

10

S-ar putea să vă placă și