6

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?

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 1
    See offsetof macro. Only for POD types! – Alex F Jul 23 '12 at 07:01
  • For each class type, every data member will be at fixed offset value. This is not the starting address of the object as compiler puts some stuff at the top. But this will be different when you have virtual functions as compiler needs to put the vptr* in the object as well. So I don't its generally good practice to do this way. – madu Jul 23 '12 at 07:15
  • 1
    may be related: http://stackoverflow.com/questions/8240273/a-portable-way-to-calculate-pointer-to-the-whole-structure-using-pointer-to-a-fi – user396672 Jul 23 '12 at 07:54

2 Answers2

9

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?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Unfortunately No.i have a pointer to the last leaf of the tree i only have parent key and using the parent key i can get the list of children that it has which is a list and which is a meber of the class.This can be said as a tree but it is not an actual tree implementation. – Vijay Jul 23 '12 at 07:16
  • @peter That’s unclear. But the answer remains the same. You *cannot* use a pointer-to-member as a pointer to class. Doing so leads to invalid C++ code that will break. – Konrad Rudolph Jul 23 '12 at 08:40
1

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).

Markus Pilman
  • 3,104
  • 3
  • 22
  • 31