Alright people. I need some brain power.
My professor gave us this example in class, and explained what the output would be. It didn't make sense to me; I assumed he was nuts (as he has made numerous mistakes with his code in class before), so I copied down the example and brought it home and compiled it. Sure enough, it does exactly what he said it would do.
So I'm thoroughly confused. I asked my programmer-god boyfriend, but he's writing a history essay and doesn't have time to look at it yet, so I thought I would pose the question here.
The example :
I'm not going to tell you what it does at this point, because that ruins some of the ... intrigue. Try to decide for yourself what you think it should do, and then compile it for yourself and see if you were right. If you want me to confirm that you got the same output I did, feel free to post your output and I'll confirm it.
If you think you understand why the program does what it does, feel free to let me know. I think I have a reasoning for the output from the first version of main(), but I have no idea regarding the second version.
If you are interested, my thoughts are :
(Please excuse my personification of these entities... it drives my boyfriend insane)
The only thing I can figure is that since f() is only defined in A, it begins its search for p() and q() by searching A's member functions. When it gets to q(), it notices that it is virtual and says "oh, I guess I'd better go see if B has its own version of q(). Hey look, it does. I'd better use that." But since p() is not virtual, it doesn't notice that it needs to update it.
I have no explanation for what happens in the second half, however.
Oh. And in case it matters, this example was used during a discussion of dynamic binding. And I'm sure it matters, I'm just not sure how.
Thanks for any help you can give!
My professor gave us this example in class, and explained what the output would be. It didn't make sense to me; I assumed he was nuts (as he has made numerous mistakes with his code in class before), so I copied down the example and brought it home and compiled it. Sure enough, it does exactly what he said it would do.
So I'm thoroughly confused. I asked my programmer-god boyfriend, but he's writing a history essay and doesn't have time to look at it yet, so I thought I would pose the question here.
The example :
#includeclass A { public: void p() { std::cout << "A::p\n"; } virtual void q() { std::cout << "A::q\n"; } void f() { p(); q(); } }; class B : public A { public: void p() { std::cout << "B::p\n"; } void q() { std::cout << "B::q\n"; } }; int main() { A a; B b; a.f(); b.f(); a = b; a.f(); // Alternate version of main; { std::cout << "\n This is a different version. Is it the same?\n\n"; A* a = new A; B* b = new B; a->f(); b->f(); a = b; a->f(); } return 0; }
I'm not going to tell you what it does at this point, because that ruins some of the ... intrigue. Try to decide for yourself what you think it should do, and then compile it for yourself and see if you were right. If you want me to confirm that you got the same output I did, feel free to post your output and I'll confirm it.
If you think you understand why the program does what it does, feel free to let me know. I think I have a reasoning for the output from the first version of main(), but I have no idea regarding the second version.
If you are interested, my thoughts are :
(Please excuse my personification of these entities... it drives my boyfriend insane)
The only thing I can figure is that since f() is only defined in A, it begins its search for p() and q() by searching A's member functions. When it gets to q(), it notices that it is virtual and says "oh, I guess I'd better go see if B has its own version of q(). Hey look, it does. I'd better use that." But since p() is not virtual, it doesn't notice that it needs to update it.
I have no explanation for what happens in the second half, however.
Oh. And in case it matters, this example was used during a discussion of dynamic binding. And I'm sure it matters, I'm just not sure how.
Thanks for any help you can give!
