Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!ginosko!uunet!jvc!lpw
From: lpw@jvc.UUCP (Lance Welsh)
Newsgroups: comp.lang.c++
Subject: Q: Object pointer arithmetic?
Keywords: what if object and type size is different?
Message-ID: <543@jvc.UUCP>
Date: 26 Sep 89 18:33:51 GMT
Organization: JVC Laboratory of America
Lines: 26


How does pointer arithmetic work when:
  - the type of the pointer is of a base class type, and
  - the object pointed to is derived from that type, and
  - the size of the derived type changed by introducing new member variables?

For Instance,
#include 
class B { public: int b; B() { b=1; } };
class D : public B { public: int d; D() { d=2; } };
const int ARRAY_SIZE=10;
main ()
{ D d[ARRAY_SIZE];
  for (B* p=d; p<&d[ARRAY_SIZE]; p++)  // will p increment by 4 or 8?
    printf ("p=%x b=%d (size=%d)\n", p, p->b, sizeof(*p));
  }

Is this the same question as how does sizeof work in similar circumstances?

If it is said that it is OK for sizeof to compute its value with compile-time
type information, ignoring what it may point at during run-time, OK.
But that does not seem to work so well with pointer arithmetic.

Just curious - apologies if it has been covered before.

-Lance P. Welsh  uunet!jvc!lpw  JVC Laboratory of America  (408) 988-4675