>Which of the following dynamically allocates an object of type ourClass?
e, none of the above.
>In the second line, which version of mem() is accessed?
c, this code causes an error.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>i made a typo on the first question
You made a typo on both questions.
>is that mean the answer is "C"
Yes, that's more like it.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>i don't think i made any mis typo on the second question
Then the worksheet had a typo. dptr is not the same as dPtr, and the example would fail to compile.
Base *dPtr = new Derived;
dPtr -> mem();
A pointer has a real type and a virtual type. The real type of dPtr is Base (the type it was declared as), and the virtual type is Derived (the actual object type pointed to). Unless a function is declared as virtual, the real type will be used, so dPtr->mem() will call Base::mem since mem isn't virtual.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>if you look at my original question, i stated that
Yes, I read it, but thanks for the reminder anyway.
>the base class mem() is not virtual so dPtr -> mem(); will call Base::mem??
Seeing as how this is a variation of what I said:
so dPtr->mem() will call Base::mem since mem isn't virtual.
It seems like you understand. The real type of dPtr is Base because it was declared as a pointer to Base. mem isn't a polymorphic function, and dPtr has no way of knowing that it actually points to an object of Derived. Therefore, Base::mem is used instead of Derived::mem.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401