Archived

This topic is now archived and is closed to further replies.

Xtreme

The address of Classes

Recommended Posts

hi guys & gals Firstly, i apologize if the following is quite obvious but it is not quite clear for me. The problem is, I create objects s1, s2 and s3. I was wondering why s3 points to the same address as s1 even though as far as I can tell, it does a member-wise copy, but isnt it an object in its own right? When I modify the x value in s1 object, I can see that the x value in s3 object hasnt changed which goes along with my theory.. BUT why are the addresses of s1 and s3 still the same? I'm a bit confused in that respect. Any ideas? Here is the source:
// inheritence3.cpp : Defines the entry point for the console application.

//


#include "stdafx.h"
#include <iostream>
using namespace std;

class SuperClass
{
private:
	int x, y;
	unsigned long* Address;
public:
	//SuperClass default constructor

	//initialise x and y variables to 0.

	//store the address of this object as well.

	SuperClass() : x(0), y(0) 
	{ 
		//cout << this;

		Address = (unsigned long *)this;
		cout << Address << ": SuperClass default constructor called\n"; 
	}

	//SuperClass destructor 

	~SuperClass() { cout << Address << ": SuperClass destructor called\n"; } 

	//Modify x value

	void ModifyX(int x)
	{
		(*this).x = x;
	}

	//Display x value

	void DisplayX()
	{
		cout << Address << ": X= " << x << endl;
	}

	//Display the SuperClass's address

	void DisplayAddress()
	{
		cout << "Address of SuperClass object: " << Address << endl;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	SuperClass s1, s2; //initialise 2 objects

	s1.ModifyX(10); // s1 contains x=10

	SuperClass s3(s1); //s3 contains x=10

	s3.DisplayX(); //x=10

	s1.ModifyX(20); //s1 now contains x=20

	s1.DisplayX(); //x=20

	s3.DisplayX(); //x=10


	return 0;
}



And the output:
   
0012FEC0: SuperClass default constructor called
0012FEAC: SuperClass default constructor called
0012FEC0: X= 10
0012FEC0: X= 20
0012FEC0: X= 10 
0012FEC0: SuperClass destructor called
0012FEAC: SuperClass destructor called
0012FEC0: SuperClass destructor called
[edited by - Xtreme on October 16, 2003 5:46:17 AM]

Share this post


Link to post
Share on other sites
if using dynamic variables, u should impelment a copy constructor and an assignemnt operator.


SuperClass::SuperClass(const SuperClass& lRhs)
{
if(this != &lRhs)
{
// copy x,y values

this->Address = this;
}
}


like this? :D
correct if \me wrong

Share this post


Link to post
Share on other sites
As you'll note from the output, the default constructor is called twice even though you have 3 instances of the class.

This will be for instances s1 and s2.

As you stated, s3 will be a member-wise copy of s1, therefore its Address variable will be the same as that s1. As its default constructor isn't called it won't get set to the right address.

Try this to see if it makes a difference


void DisplayAddress()
{
cout << "Address of SuperClass object: " << (unsigned long *)this << endl;
}


[edited by - gav on October 16, 2003 5:58:09 AM]

Share this post


Link to post
Share on other sites
umm.. not sure what you are showing me here.

THe issue is if the output displays different x values why do they point to the SAME address?

Share this post


Link to post
Share on other sites
Gav,

Now its clear!! Since im doing a member-wise copy, im also copying the contents of "Address" variable across to s3.
So, s3 definitely should have a different address Right?

Share this post


Link to post
Share on other sites
Thanks Guys!!
The following code shows that they are different addresses after all.


// inheritence3.cpp : Defines the entry point for the console application.

//


#include "stdafx.h"
#include <iostream>
using namespace std;

class SuperClass
{
private:
int x, y;
public:
//SuperClass default constructor

//initialise x and y variables to 0.

//store the address of this object as well.

SuperClass() : x(0), y(0)
{
//printf("%08X\n",this);

//printf("%p\n", this);

cout << (unsigned long *)this << ": SuperClass default constructor called\n";
}

//SuperClass destructor

~SuperClass() { cout << (unsigned long *)this << ": SuperClass destructor called\n"; }

//Modify x value

void ModifyX(int x)
{
(*this).x = x;
}

//Display x value

void DisplayX()
{
cout << (unsigned long *)this << ": X= " << x << endl;
}

//Display the SuperClass's address

void DisplayAddress()
{
cout << "Address of SuperClass object: " << (unsigned long *)this << endl;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
SuperClass s1, s2; //initialise 2 objects

s1.ModifyX(10); // s1 contains x=10

SuperClass s3(s1); //s3 contains x=10

s3.DisplayX(); //x=10

s1.ModifyX(20); //s1 now contains x=20

s1.DisplayX(); //x=20

s3.DisplayX(); //x=10


return 0;
}



[edited by - Xtreme on October 16, 2003 6:08:12 AM]

Share this post


Link to post
Share on other sites