I am working on one of the problems with my application. Here, the problem I am facing is some functions that I need to call using the pointer of a class object.
But the main problem is that I don’t have a class pointer with me, instead I have the value of a member variable (let's say its list of values). after that I did a little test with the code below.
using namespace std;
class Person {
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
string getName() {
return name;
}
int getAge() {
return age;
}
void Print()
{
printf("This address is %x\n",this);
printf("age adress is %x\n",&age);
}
private:
int age;
string name;
};
int main() {
cout << "Creating a person..." << endl;
Person *johnDoe=new Person("John Doe", 25);
cout << "Person name: " << johnDoe->getName() << endl;
cout << "Person age: " << johnDoe->getAge() << endl;
johnDoe->Print();
delete johnDoe;
return 0;
}
Coutput Runtime:
> ./a.out
Creating a person...
Person name: John Doe
Person age: 25
This address is 72918
age adress is 72918
Now I doubt:
Is it guaranteed that the address of a member variable of a class always points to the address of an object? Can I use this address in case I need to use a pointer to call other basic api functions?
I saw this when I googled?
(C1x §6.7.2.1.13: " , , ... . , ." )
++ ?