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

0
7
Adv1


Adv2

Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Prerequisite:  Courses and Objects in C++

The situation of an object within the reminiscence is known as its handle. Addressing is a vital a part of C++, it allows us to make use of any ingredient as a reference and maintains the individuality of all the weather whether or not it’s any variable, object, or container. On this article, we are going to see tips on how to entry the handle of an object.

Accessing & Printing the Tackle of an Object

There are three strategies to entry the handle of an object:

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

1. Utilizing the addressof operator

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

Instance:

C++

#embody <iostream>

#embody <reminiscence>

utilizing namespace std;

  

class GFG {

};

  

int essential()

{

    GFG obj1 = GFG();

    GFG obj2 = GFG();

  

    cout << "Tackle of this object 1 is "

         << addressof(obj1) << endl;

    cout << "Tackle of this object 2 is "

         << addressof(obj2) << endl;

  

    return 0;

}

Output

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

2. Get the handle of the item utilizing this operator

1. this operator factors to the handle of an object

2. this operator can solely be accessed by the member operate of this class

To know extra about this operator discuss with ‘this’ in C++.

Instance:

C++

#embody <bits/stdc++.h>

utilizing namespace std;

  

class GFG {

  

public:

    void printAddress()

    {

        cout << "Tackle of this object is " << this

             << endl;

    }

};

  

signed essential()

{

  

    GFG obj1 = GFG();

    GFG obj2 = GFG();

  

    obj1.printAddress();

    obj2.printAddress();

  

    return 0;

}

Output

Tackle of this object is 0x7ffdac7b2d0e
Tackle of this object is 0x7ffdac7b2d0f

3. Utilizing the ‘&’ operator

One of many customary methods can be utilizing pointers. We all know {that a} pointer shops the handle of variables, objects, arrays, and so on. 

Instance:

C++

#embody <iostream>

utilizing namespace std;

  

class GFG {

public:

    int x;

};

  

int essential()

{

    GFG obj1 = GFG();

    GFG obj2 = GFG();

  

    cout << "Tackle of object 1 n";

    cout << &obj1 << endl;

  

    cout << "Tackle of object 2n";

    cout << &obj2 << endl;

  

    return 0;

}

Output

Tackle of object 1 
0x7ffe2de8c45e
Tackle of object 2
0x7ffe2de8c45f

4. Utilizing a pointer to entry the handle of an object

The pointer can allow using dynamic reminiscence allocation. The thing is saved contained in the heap reminiscence. So, to entry the handle we will use the property of pointer the place pointer_name shops the handle and *pointer_name shops the worth. to know extra about pointers discuss with Pointers in C++.

Instance:

C++

#embody <iostream>

utilizing namespace std;

  

class GFG {

public:

    int x;

};

  

int essential()

{

    GFG* obj1 = new GFG();

    GFG* obj2 = new GFG();

  

    cout << "Tackle of object 1 n";

    cout << obj1 << endl;

  

    cout << "Tackle of object 2n";

    cout << obj2 << endl;

  

    return 0;

}

Output

Tackle of object 1 
0x25e8010
Tackle of object 2
0x25e8030

Adv3