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:
- Utilizing the addressof operator
- Utilizing this operator
- Utilizing ‘&’ operator
- Utilizing pointer
1. Utilizing the addressof operator
C++ addressof operator returns the handle of an object.
Instance:
C++
|
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++
|
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++
|
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++
|
Tackle of object 1 0x25e8010 Tackle of object 2 0x25e8030