Printing the Address of an Object of Class in C++
Last Updated :
13 Dec, 2022
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:
- Using the addressof operator
- Using this operator
- Using '&' operator
- 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;
}
OutputAddress 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;
}
OutputAddress 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;
}
OutputAddress 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;
}
OutputAddress of object 1
0x25e8010
Address of object 2
0x25e8030
Similar Reads
How to get the memory address of an object in Python In Python, everything is an object, from variables to lists and dictionaries everything is treated as objects. In this article, we are going to get the memory address of an object in Python. Method 1: Using id() We can get an address using the id() function. id() function gives the address of the pa
2 min read
Creating a Vector of Class Objects in C++ Prerequisites: Object Oriented Programming in C++Vector in C++ Class is a user-defined data type that can be accessed by creating objects or instances of that class. A vector is a type of container which can store elements of a similar type. Vector of Class The vector of class objects is an example
3 min read
std::to_address in C++ with Examples The std::to_address, introduced in C++20, is used to obtain the address represented by the specified pointer without forming a reference to the pointee. The existing std::addressof cannot do std::addressof(*ptr) because *ptr isnât always an object. The std::to_address solves these issues for us. Syn
2 min read
Address Operator & in C The Address Operator in C is a special unary operator that returns the address of a variable. It is denoted as the Ampersand Symbol ( & ). This operator returns an integer value which is the address of its operand in the memory. We can use the address operator (&) with any kind of variables,
3 min read
Why is the Size of an Empty Class Not Zero in C++? When the structure was introduced in C, there was no concept of Objects at that time. So, according to the C standard, it was decided to keep the size of the empty structure to zero. In C++, the Size of an empty structure/class is one byte as to call a function at least empty structure/class should
4 min read