+ 1

how can i get address of an object in c++ ?

7th Mar 2017, 8:27 PM
Navid Tak
Navid Tak - avatar
1 Answer
+ 2
You can use the address-of operator (&). This denotes an address in memory. You can say "cout << &obj;" to get the address of an object. You can also store it to a pointer, and then get the address that way. Here is a complete example of using a pointer to get an object's address, and execute one of the object's functions: #include <iostream> using namespace std; int main() { class boy { public: void sayhello(){ cout << "hi" << endl; } }; boy one; one.sayhello(); cout << &one << endl; boy *ptr; ptr = &one; cout << ptr << endl; ptr->sayhello(); return 0; }
7th Mar 2017, 9:25 PM
Rain
Rain - avatar