Home » C++ programs » C++ Class and Objects programs

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

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

Example:

#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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.