C++ Pointers and Objects

C++ allows you to have pointers to objects. The pointers pointing to objects are referred to as "object pointers."

C++ Declaration and Use of Object Pointers

Object pointers, like other pointers, are declared by placing in front of the object pointer's name. It takes the following general form:

class-name ∗object-pointer;

where class-name is the name of an already-defined class, and object-pointer is the pointer to an object of this class type. For example, to declare optr as an object pointer of the Sample class type, we shall write

Sample ∗optr;

where Sample is a predefined class. When using an object pointer to access members of a class, the arrow operator (->) is used instead of the dot operator.

The following program illustrates how to access an object given a pointer to it. This C++ program illustrates the use of object pointers.

#include <iostream>
using namespace std;

class Time
{
   short int hh, mm, ss;

public:
   Time()
   {
      hh = mm = ss = 0;
   }
   void getdata(int i, int j, int k)
   {
      hh = i;
      mm = j;
      ss = k;
   }
   void prndata(void)
   {
      cout << "\nThe time is " << hh << ":" << mm << ":" << ss << ".\n";
   }
};

int main()
{
   Time T1, *tptr;

   cout << "Initializing data members using the object with values 12, 22, and 11\n";
   T1.getdata(12, 22, 11);
   cout << "Printing members using the object ";
   T1.prndata();
   tptr = &T1;
   cout << "Printing members using the object pointer ";
   tptr->prndata();

   cout << "\nInitializing data members using the object pointer, with values 15, 10, and 16\n";
   tptr->getdata(15, 10, 16);
   cout << "printing members using the object ";
   T1.prndata();
   cout << "Printing members using the object pointer ";
   tptr->prndata();

   return 0;
}

On compiling and executing the above program, it will produce the following output:

c++ pointers and objects

The above program 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.

As you know, when a pointer is incremented, it points to the next element of its type. The same is true of pointing to an object.

C++ "this" pointer

When you define a class, the member functions are created and stored in memory only once. That is, only one copy of member functions is kept, which is shared by all class objects. Only space for data members is allocated to each object separately (see the following figure).

cpp pointers this

This has an associated problem. If only one instance of a member function exists, how does it know which object's data member is to be manipulated? For example, if member function3 is capable of changing the value of data member2 and we want to change the value of data member2 of object1. How did the member function3 determine which object's data in member2 needed to be changed?

The answer to this problem is "this" pointer. When a member function is called, it is automatically passed an implicit (in-built) argument that is a pointer to the object that invoked the function. This pointer is called "this." That is, if object1 is invoking member function3, then an implicit argument is passed to member function3 that points to object1, i.e., "this" pointer now points to object1. The "this" pointer can be thought of as analogous to the ATM card. For instance, in a bank, there are many accounts. The account holders can withdraw money or view their bank statements through automatic teller machines. Now, these ATMs can withdraw money from any account in the bank, but which account are they supposed to work on? This is resolved by the ATM card, which gives the identification of the user and his accounts, from which the amount is withdrawn.

Similarly, the "this" pointer is the ATM card for objects, which identifies the currently-calling object. The "this" pointer stores the address of the currently-called object. To understand this, consider the following example program (the following program illustrates the functioning of "this" pointer):

#include <iostream>
#include <cstring>
using namespace std;

class Salesman
{
   char name[80];

public:
   Salesman(char *s)
   {
      strcpy(name, s);
   }
   void prnobject()
   {
      cout<<"Object: "<<this->name<<endl;
   }
};

int main()
{
   Salesman William("William");
   Salesman Edwin("Edwin");

   William.prnobject();
   Edwin.prnobject();

   return 0;
}

The output should be:

Object: William
Object: Edwin

It is obvious from the above output that when the object William invokes the member function prnobject(), this points to William and thus prints the name data member of William. Similarly, this points to an Edwin object when it invokes prnobject().

Remembers, this pointer points to the object invoked by prnobject(). Thus, this->name refers to that object's copy of name. The (this) refers to the object itself. With (*this), the dot (.) operator should be used in order to access the member elements of the object, as this is not a pointer to the object, it is the object itself.

C++ Quiz


« Previous Tutorial Next Tutorial »


Liked this post? Share it!