How can we Print the address of object of class in the member function of that class in C++?
class A
{
int x;
private:
A(){x=2;}
}
int main()
{
A B;
return 0;
}
How to print the address of B in member function or in main().
To print the address of a variable, we use "%p" specifier in C programming language. There are two ways to get the address of the variable: By using "address of" (&) operator. By using pointer variable.
We can get an address using the id() function. id() function gives the address of the particular object.
The location of an object in the memory is called its address.
The __repr__() method returns the object's printable representation in the form of a string. It, by default, returns the name of the object's class and the address of the object. When we print an object in Python using the print() function, the object's __str__() method is called.
Just add
#include <iostream>
at the top of the file and the following line at the bottom of main
:
std::cout << &B << std::endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With