Access the address of an object using 'this' pointer in C++

C++ class | Access the address of an object using this pointer: Here, we are going to learn how to access address of the objects using this pointer in C++?
Submitted by IncludeHelp, on September 28, 2018 [Last updated : March 01, 2023]

Accessing the address of an object using 'this' pointer

In C++, it is allowed to get the address of the object by using ‘this’ pointer. Read more: "this pointer" in C++.

C++ program to access the address of an object using 'this' pointer

#include <iostream>
using namespace std;

class ExampleThis {
public:
    ExampleThis* address(void)
    {
        return this;
    }
};

//Main functionn
int main()
{
    //creating objects
    ExampleThis Ex1, Ex2, Ex3;

    //printing the object's address
    cout << "Address of object Ex1: " << Ex1.address() << endl;
    cout << "Address of object Ex2: " << Ex2.address() << endl;
    cout << "Address of object Ex3: " << Ex3.address() << endl;

    return 0;
}

Output

Address of object Ex1: 0x7ffd6550aa8d
Address of object Ex2: 0x7ffd6550aa8e
Address of object Ex3: 0x7ffd6550aa8f


Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.