--- 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?
>
> Here's a small code that demonstrates it.
>
> #include<iostream>
> using namespace std;
>
> class Under
> {
> public:
> void ImUnder() {cout << "I'm Under" << endl;};
> };
>
> class Over : public virtual Under
> {
> public:
> void ImOver() {cout << "I'm Over" << endl;};
> };
>
> int main()
> {
> cout << "Test inheritance ---------" << endl;
> Over o;
> o.ImUnder();
> o.ImOver();
>
> cout << "Test access to objects on the heap ----
> pUnder to Under object"
> << endl;
> Under *pu;
> pu = new Under; // This pu does not have Over in
> scope.
> pu->ImUnder();
> cout << "Test access to objects on the heap ----
> pOver to Over object"
> << endl;
> Over *po;
> po = new Over;
> po->ImOver();
> po->ImUnder();
> delete pu;
> cout << "The following is the part I don't
> understand" << endl;
> 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?
> pu->ImUnder();
> // pu->ImOver(); // This pu still does not have
> Over in scope
> return(0);
> }
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com