Open In App

Printing the Address of an Object of Class in C++

Last Updated : 13 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite:  Classes and Objects in C++

The location of an object in the memory is called its address. Addressing is a necessary part of C++, it enables us to use any element as a reference and maintains the uniqueness of all the elements whether it is any variable, object, or container. In this article, we will see how to access the address of an object.

Accessing & Printing the Address of an Object

There are three methods to access the address of an object:

  1. Using the addressof operator
  2. Using this operator
  3. Using ‘&’ operator
  4. Using pointer

1. Using the addressof operator

C++ addressof operator returns the address of an object. 

Example:

C++




// C++ program to print address of an object
#include <iostream>
#include <memory>
using namespace std;
 
class GFG {
};
 
int main()
{
    GFG obj1 = GFG();
    GFG obj2 = GFG();
 
    cout << "Address of this object 1 is "
         << addressof(obj1) << endl;
    cout << "Address of this object 2 is "
         << addressof(obj2) << endl;
 
    return 0;
}


Output

Address of this object 1 is 0x7ffd8d7a5ece
Address of this object 2 is 0x7ffd8d7a5ecf

2. Get the address of the object using this operator

1. this operator points to the address of an object

2. this operator can only be accessed by the member function of this class

To know more about this operator refer to ‘this’ in C++.

Example:

C++




// C++ program to print address of an object
#include <bits/stdc++.h>
using namespace std;
 
class GFG {
 
public:
    void printAddress()
    {
        cout << "Address of this object is " << this
             << endl;
    }
};
 
signed main()
{
 
    GFG obj1 = GFG();
    GFG obj2 = GFG();
 
    obj1.printAddress();
    obj2.printAddress();
 
    return 0;
}


Output

Address of this object is 0x7ffdac7b2d0e
Address of this object is 0x7ffdac7b2d0f

3. Using the ‘&’ operator

One of the standard ways will be using pointers. We know that a pointer stores the address of variables, objects, arrays, etc. 

Example:

C++




// C++ program to print address of an object
// Using & operator
#include <iostream>
using namespace std;
 
class GFG {
public:
    int x;
};
 
int main()
{
    GFG obj1 = GFG();
    GFG obj2 = GFG();
 
    cout << "Address of object 1 \n";
    cout << &obj1 << endl;
 
    cout << "Address of object 2\n";
    cout << &obj2 << endl;
 
    return 0;
}


Output

Address of object 1 
0x7ffe2de8c45e
Address of object 2
0x7ffe2de8c45f

4. Using a pointer to access the address of an object

The pointer can enable the use of dynamic memory allocation. The object is stored inside the heap memory. So, to access the address we can use the property of pointer where pointer_name stores the address and *pointer_name stores the value. to know more about pointers refer to Pointers in C++.

Example:

C++




// C++ program to print address of an object
// Using pointer
#include <iostream>
using namespace std;
 
class GFG {
public:
    int x;
};
 
int main()
{
    GFG* obj1 = new GFG();
    GFG* obj2 = new GFG();
 
    cout << "Address of object 1 \n";
    cout << obj1 << endl;
 
    cout << "Address of object 2\n";
    cout << obj2 << endl;
 
    return 0;
}


Output

Address of object 1 
0x25e8010
Address of object 2
0x25e8030


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads