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?
The location of an object in the memory is called its address. In C++ there is an address (or referencing ) operator, &, which allows you to obtain an object's address. The value returned by this address operator indicates the location of an object in memory.
To use pointers in C, we must understand below two operators. To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x.
The address is the memory location where the entity is stored. Every block of code in the program has its own memory location in the program. Which means like any variable or object methods and functions also have memory address.
In terms of C programming, an object is implemented as a set of data members packed in a struct , and a set of related operations. With multiple instances, the data for an object are replicated for each occurrence of the object.
Is it guaranteed that the address of the class member variable always points to the address of the object?
No. In C++, there is potentially stuff before the first member (in particular for derived classes and polymorphic classes). What exactly there is is implementation defined.
Can i use this address in case i need to use the pointer for calling other core api functions?
No, and you don’t need to: whenever you have access to members, you also have access to this
, no?
This is true also for classes unless they have any virtual functions. Also you should take care if you have inheritance (not sure how exactly the rules are there).
In general, this is a bad idea though. Why do you want to do that? You rely on the structure of the class (i.e. your code will break if you by accident add a member before age) and you give away encapsulation (no other class should have a pointer to age anyway, since it is private - you would leak you object representation).
Btw: Are you a Java programmer? Your constructor looks like you are, since you do not initialize your object correctly. You should do it like this:
Person(String n, int a) : age(a), name(s) {}
(otherwise the compiler will first call the default contstructors of int and string and then call operator= on them - this is not efficient).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With