Q:

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

belongs to collection: C++ programs on various topics

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Create a class with public data members only in C+... >>
<< Accessing Member Function by pointer in C++...