How to access the address of an object in C++?

The following tutorial aims at ways by which the users can access the address of an object in C++. The instructions contain two ways which can be used to access the address of the object, we will be studying both of them.

C++ program to access the address of an object

Let’s study the methods step by step.

  1. Address-of operator
  2. ‘this’ operator

Method 1: Using Address-of or ‘&’ operator

This ‘address-of’ operator is a C++ mechanism that returns the address of the object when called with the object. It is a unary operator and the address returned by the operator is known a pointer as it points to the location of the object. The operator can be applied to fundamental, class, structure, union types of data or array references. Any constant term which does not contain the address-of operator can be added to subtracted from the address-of expression.

The operator is generally used :

  1. To pass parameters as reference
  2. To establish pointers to objects

Syntax :

&object;

 

Declaring the variables

Code :

int a;
int *pointer_to_a;

a : ‘a’ is an integer object.

pointer_to_a : It is an integer pointer which we will be using to store the address of object a.

 

Establishing the pointer

Code :

pointer_to_a = &a;

cout<<"\nAddress of integer object a : "<<pointer_to_a;

The Address-of operator is used with object a. It returns the pointer to location of a. Or in simple terms, it returns the address of object a. The address is stored in pointer_to_a integer pointer.
We display the address for reference of user here.

Output :

Address of integer object a : 0x61ff04

 

Method 2 : Using ‘this’ operator

‘this’ operator is used with class objects. It contains the address of the current object. Basically, it points to the current object of the class. When any class object is declared, every object has a separate copy of the data members of class but the functions of the class are common to all the objects. When the names of the variables for different objects of the same class are same, ambiguity is created.  Therefore, the ‘this’ operator is used to point to the current object for any further implementation. The type of ‘this’ pointer depends upon the class it is used in.
For example :

  1. If the class name is ‘test_class’ then the ‘this’ operator will be of type ‘test_class*’
  2. If the class is ‘const test_class’ then the ‘this’ operator will also be of the type ‘const test_class*’

 

Class definition

Code :

class example_class
{
    public:
            example_class* address_of_object(void){

                return this;
            }

};

The example_class is declared. It has one member function.
example_class* address_of_object(void) { } : The return data type of this function is of a type pointer to the class object. The function does not require any parameters. The ‘this’ pointer used, returns the address of the current object, which is the class object that is used to call this function in main().

 

Declaring the variables

Code :

example_class object1;
example_class *pointer_to_object1;

object1 : It is an object of example_class class.

pointer_to_object1 : It is a pointer of the same example_class class which will be used to point to object1 later.

 

Establishing the pointer

Code :

pointer_to_object1 = object1.address_of_object() ;

cout << "\n\nAddress of example_class object 1 : " << pointer_to_object1;

The object1 is used to call the class function address_of_object(). The function returns the address of the calling object. It is stored in the object pointer pointer_to_object1.
We display the address for reference of user here.

Output :

Address of example_class object 1 : 0x61ff03

 

Complete Code

#include<iostream>

using namespace std;

class example_class
{
    public:
            example_class* address_of_object(void){

                return this;          //Using 'this' pointer
            }

};


int main()
{

        int a;
        int *pointer_to_a;

        pointer_to_a = &a;          //Using '&' pointer

        cout<<"\nAddress of integer object a : "<<pointer_to_a;


        example_class object1;
        example_class *pointer_to_object1;

        pointer_to_object1 = object1.address_of_object() ;          //Using 'this' pointer

        cout << "\n\nAddress of example_class object 1 : " << pointer_to_object1;
        cout << "\n";

        return 0;
}

Output :

Address of integer object a : 0x61ff04

Address of example_class object 1 : 0x61ff03

 

You may also learn :

Pointers to Functions in C++

C++ Program to Dereference a Pointer

Feel free to ask your questions or leave comments in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *