Documente Academic
Documente Profesional
Documente Cultură
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:
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.
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.
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;
2
AIM
ALGORITHM
3. Declare pointer variable named *ip and actual variable named var
SOURCE CODE
#include <iostream>
int main ()
{
int var = 20; // actual variable declaration.
int *ip; // pointer variable
3
cout << *ip << endl;
return 0;
}
OUTPUT
When the above code is compiled and executed, it produces result something as follows:
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 points to a value that does not change and is declared as :
4
const type * name
EXAMPLE
Pointer to constant can not be used to change the value being pointed to. Therefore :
char ch = A;
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 :
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;
p = &ch2; will throw an error since address stored in p can not be changed
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
6
ALGORITHM
3. Using overloaded operator function return the value of pointer to function named
show.
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;
}
MyClass show()
{
cout<<The elements are:\n cout<i<<\n<f; //accessing data members using this
}
};
int main()
{
MyClass a;
OUTPUT
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