Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we print the address of object of class in C++?

Tags:

c++

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().

like image 594
techno Avatar asked Dec 26 '13 14:12

techno


People also ask

How do I print addresses of objects?

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.

How do I find the address of a class object?

We can get an address using the id() function. id() function gives the address of the particular object.

What is the address of an object?

The location of an object in the memory is called its address.

How we can print the address of an instance of a class?

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.


1 Answers

Just add

#include <iostream>

at the top of the file and the following line at the bottom of main:

std::cout << &B << std::endl;
like image 127
Michael Schlottke-Lakemper Avatar answered Sep 23 '22 17:09

Michael Schlottke-Lakemper