--- Jacob Lund Fisker <[EMAIL PROTECTED]> wrote: > On Wed, 17 Oct 2007, Mickey Mathieson wrote: > > > --- Jacob Lund Fisker <[EMAIL PROTECTED]> wrote: > > > >> Could someone tell me why/how it is possible to > >> declare a pointer to a > >> base class and use that pointer to declare and > point > >> to an object an > >> inherited (larger) class on the heap. > >> > >> I guess my question is what exactly gets > allocated > >> on the heap in this > >> case? Is it an "Under" object or an "Over" > object? > > >> cout << "Test access to objects on the heap > ---- > >> pUnder to Over object > >> --- why does this work?" << endl; > >> pu = new Over; > > Are you asking why it compiles? > > > > Yes. And also, which object on the heap is pu > pointing to? It seems to me > that pointers that are declared to point to a base > class should not be > able to point to inherited classes too without > problems with e.g. pointer > arithmetic?! > a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism.
The pu pointer is pointing to the Over object. Mickey M. Construction Partner Inc. http://www.constructionpartner.com __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
