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