Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    cpp
  » Keyword: this (pointer)
      by Valmont
 Page 3 of 3 
< Previous    

(Login to remove green text ads)
About Objects And Addresses
About Objects and addresses


Whenever an instance of a class is made, an object is created in the memory of the computer. So the object (= instance
of a class) actually uses memory. Since objects use memory, they also use addresses in the memory. Each object
has a starting address. Below is a way to retrieve the starting address of an object.
Code:
#include <iostream>
#include <string>

using namespace std; //Ok for this small project.

class CSomeClass
{
public:
  void Print(string text)
  {
    cout<<text<<endl<<endl;
  }
};


int main()
{
  CSomeClass obj_1;
  cout<<"I am obj_1 and my address is: "<<&obj_1<<endl; //Observe the ampersand (&)!

  obj_1.Print ("Object 1 made me print this");

  return 0;
}
The address given is a hexadecimal value. 0x means hexadecimal.
The last thing you need to know is that every object has it's own space in the memory of a computer. So it has it's own
unique address. This is a very important property of an object!
Below shows the code to demonstrate this. This is all you need to know about objects on their own.
Code:
#include <iostream>
#include <string>

using namespace std;

class CSomeClass
{
public:
  void Print(string text)
  {
    cout<<text<<endl<<endl;
  }
};


int main()
{
  CSomeClass obj_1;
  cout<<"I am obj_1 and my address is: "<<&obj_1<<endl; //Observe the ampersand (&)!
  CSomeClass obj_2;
  cout<<"I am obj_2 and my address is: "<<&obj_2<<endl;

  obj_1.Print ("Object 1 made me print this");
  obj_2.Print ("Object 2 made me print this");

  return 0;
}
Behaviour Of System When Calling An Object

A member function is a function (method) specified in a class (or struct). Beginning C++ students work with free functions
to start with. Free functions are not members of a class.
This tutorial covers only member functions.
When an object calls a member function, the computer stores the address of the calling object in a temporary and invisible constant variable. A constant variable can not be changed. The designer of C++ (B. Soustroup) gave a name to this invisible constant variable:
this
So this holds the address of the calling object.
When multiple instances (objects) of a class exist, only the address of the calling object will be stored in this.

Two instances of a class, NOT calling a member function:


If you ran one of the codes presented above, where the address of an object is displayed, then you know what those weird numbers mean in the blue boxes. The red colored lines and box show that none of the objects' address can be passed to the this variable YET.

Next step:
An object calls a member function.
Then the address of the calling object is stored in this .
Then this is passed to the called member function internally.
And THIS is the reason we why a member function knows who the caller was!

Observe obj_2 calling CSomeClass::Print():



That is all there is to tell about this. Our next step is to demonstrate the existence of this in simple code.

A little bit of code again
Code:
#include <iostream>
#include <string>

using namespace std;

class CSomeClass
{
public:
  void Print(string text)
  {
    cout<<text<<endl;
    cout<<"The address of the calling object is: "<<this<<endl<<endl;
  }
};


int main(int argc, char* argv[])
{
  CSomeClass obj_1;
  CSomeClass obj_2;

  obj_1.Print ("Object 1 made me print this");
  obj_2.Print ("Object 2 made me print this");

  return 0;
}
Lets extend our class a bit. We give it a member variable (property) and an accessor function:
Code:
#include <iostream>
#include <string>

using namespace std; //Ok for this very small project.

class CSomeClass
{
private:
int m_intSomeInt;
public:
  void SetInt(int i)
  {
    m_intSomeInt = i;
    cout<<"I am SetInt(). I was called by address "<<this<<" to change m_intSomeInt!"<<endl;
    cout<<"The new value of m_intSomeInt is: "<<m_intSomeInt<<"."<<endl<<endl;
  }

  void Print(string text)
  {
    cout<<text<<endl;
    cout<<"The address of the calling object is: "<<this<<endl<<endl;
  }

};


int main(int argc, char* argv[])
{
  CSomeClass obj_1;
  CSomeClass obj_2;

  obj_1.SetInt(5);
  obj_2.SetInt(4391);

  return 0;
}
Note that
Code:
m_intSomeInt = i;
Is exactly the same as
Code:
this->m_intSomeInt = i;
Remember why? Because this is a pointer.
However, realize that *this is the actual object!

This is all one needs to know about this.

Mail to valmont_gaming@hotmail.com for comments. Comments that might lead to a better tutoring style is very welcome.




 
 Page 3 of 3 
< Previous    

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting