Pointer in C++

When an object is created from its class, the member variables and member functions are allocated memory spaces. The memory spaces have unique addresses.

Pointer is a mechanism to access these memory locations using their address rather than the name assigned to them. You will study the implications and applications of this mechanism in detail in this chapter.

Pointer is a variable which can hold the address of a memory location rather than the value at the location. Consider the following statement.

int num =84;

This statement instructs the compiler to reserve a 2-byte of memory location and puts the value 84 in that location. Assume that the compiler allocates memory location 1001 to num. Diagrammatically, the allocation can be shown as:

variable memory allocation
variable memory allocation

As the memory addresses are themselves numbers, they can be assigned to some other variable. For example, ptr be the variable to hold the address of variable num. Thus, we can access the value of num by the variable ptr. We can say “ptr points to num” as shown in the figure below.

pointer to variable

Pointers to Objects

An object of a class behaves identically as any other variable. Just as pointers can be defined in case of base C++ variables so also pointers can be defined for an object type. To create a pointer variable for the following class.

class employee {
     int code;
     char name [20] ;
public:
    inline void getdata ( )= 0 ;
    inline void display ( )= 0 ;
};
employee *abc;

This above declaration creates a pointer variable *abc that can point to any object of employee type.

Example:

class employee {
     int code;
     char name [20] ;
public:
    inline void getdata ( )
	{
		//function body
	}
    inline void display ( )
	{
		//function body
	}
};
void main()
{
	employee *abc,e1;
	abc=&e1;
	abc->getdata();
	abc->display()	
}

Pointers to Derived Classes

Polymorphism is also accomplished using pointers in C++. It allows a pointer in a base class to point to either a base class object or to any derived class object.

We can have the following Program segment show how we can assign a pointer to point to the object of the derived class.

Syntax:

class base
{
	//Data Members
	//Member Functions
};
class derived : public base
{
	//Data Members
	//Member functions
};
void main ( ) 
{
	base *ptr; 	//pointer to class base
	derived obj ;
	ptr = &obj ; //indirect reference obj to the pointer
	//Other Program statements
}

The pointer ptr points to an object of the derived class obj. But, a pointer to a derived class object may not point to a base class object without explicit casting.

For example, the following assignment statements are not valid:

void main ( )
{
	base obja;
	derived *ptr;
	ptr = &obja; //invalid.... .explicit casting required
	//Other Program statements
}

A derived class pointer cannot point to base class objects. But, it is possible by using explicit casting.

void main ( )
{
	base obj ;
	derived *ptr; // pointer of the derived class 
	ptr = (derived *) & obj; //correct reference 
	//Other Program statements
}

Related posts