I am working on one of my application issue. Here the problem i am facing is there are some bunch of function which i need to call using the pointer of a class object.
But the main problem is i donot have a class pointer with me,instead i have a member variable value(lets say its a list of values). following this i did a small test with the below code.
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's name: " << johnDoe->getName() << endl;
cout << "Person's age: " << johnDoe->getAge() << endl;
johnDoe->Print();
delete johnDoe;
return 0;
}
The coutput of the execution is :
> ./a.out
Creating a person...
Person's name: John Doe
Person's age: 25
This address is 72918
age adress is 72918
Now my doubt is :
Is it guaranteed that the address of the class member variable always points to the address of the object? Can i use this address in case i need to use the pointer for calling other core api functions?
I saw this when i googled?
(C1x §6.7.2.1.13: "A pointer to a structure object, suitably converted, points to its initial member ... and vice versa. There may be unnamed padding within as structure object, but not at its beginning.")
is this true even in case of c++ and classes?