7

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

techno
  • 101
  • 1
  • 1
  • 8
  • Note that your code throws a compile error since your constructor is private. The question is still valid with a public constructor. – talekeDskobeDa Jan 15 '19 at 13:04

12 Answers12

12

Just add

#include <iostream>

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

std::cout << &B << std::endl;
5

Inside main:

std::cout << &B << std::endl;

Inside member function:

A () {
    x=2;
    //this is a pointer to the this-object
    std::cout << this << std::endl;
}

Don't forget to include <iostream> for output.

ben
  • 5,671
  • 4
  • 27
  • 55
3

Easiest way: printf("%p", &B);

egur
  • 7,830
  • 2
  • 27
  • 47
2

Did you try

std::cout << &B;
m0skit0
  • 25,268
  • 11
  • 79
  • 127
2

To get the address of an object, use the address-of operator &.

Philipp
  • 67,764
  • 9
  • 118
  • 153
2

With C++20 you can use std::to_address:

#include <memory>
#include <iostream>

class A{};

int main()
{
    A B;
    std::cout << std::to_address(&B) << '\n'
}

Which has the advantage of being able to accept raw pointers, smart pointers, iterators, and pointer-like objects. Making it useful for cases that require generic code.

Bryan Triana
  • 43
  • 2
  • 6
1

You can try

cout << &B
gturri
  • 13,807
  • 9
  • 40
  • 57
1
#include <iostream>
using namespace std;
class A{
     public:
     A(){
      cout<<this<<endl;
     }
};

int main()
{
   A* a = new A();
   cout<<a<<endl;

   return 0;

}
0

If you are trying to print the memory address you need to convert B to a pointer when printing. So

cout << &B << endl;

The ampersand, when used before a variable, calls the pointer of the variable instead of the variable itself.

Jesse Laning
  • 490
  • 4
  • 12
0

If you mean in THAT specific class you have defined. You can't. Your constructor is private and you forgot a semi-colon at the end of your class definition, therefore, your code won't compile.

Ignoring these issues to display the pointer of an object in main you can use

std::cout << &B;

Or in a member function you can use

std::cout << this;

(Don't forget to include iostream)

ctor
  • 5,938
  • 2
  • 26
  • 37
0

this line has to be add-cout<<"address is:"<<&B;

#include <iostream>
using namespace std;

    class A
    {
     int x;

    };

  int main()
   {
   A B;
   cout<<"address is:"<<&B;
    return 0;
   }
Kavita Jain
  • 90
  • 1
  • 9
0

I see that there are a lot of answers for your question already, but I don't know why none of them point that you have a private constructor for class A, and the way the object B is being instantiated should throw a compilation error. At least, it did in my case. There is a whole lot of concepts around using private constructors, https://stackoverflow.com/a/2385764/3278350 gives a good idea.

talekeDskobeDa
  • 372
  • 2
  • 13