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
      by
 Page 3 of 6 
< Previous     Next >

(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;

//--

class CSomeClass
{
public:
  void Print(const string text) const
  {  
    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(const string text) const
  {  
    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;
}





 
 Page 3 of 6 
< Previous     Next >

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