my computer ain't likin' polymorphism

Started by
7 comments, last by Palidine 19 years, 2 months ago
I have a base class that has a non-virtual function member(). It has a derived class that implements that function in a different way. When I do this: Base* object = new Derived(); object->member(); It freezes. And not exactly freezes, but when I pull up the task manager my mouse shows up where the task manager should be, but I can't see the actual window. I click, Alt-F4, Alt-tab, and everything else I can think of and it doesn't respond. What's goin' on? I should mention that the objects are inside an std::list. Thankyou!
Advertisement
run it through a debugger and step into the function to see what's going on.

also post your code.

-me
Try taking the code out of the member() function entirely, and see if the bug goes away. You might want to try a Rebuild All too.
We really need some more code to give any more information.
You say it's non-virtual, but a derived class re-implements that method? It has to be virtual. Not that that's why it's freezing, just something that I thought should be pointed out.
This is actually a pretty basic problem. You are declaring a Base class pointer that points to your derived class. That is fine, but you can't then call functions in your derived class using that base class pointer without a cast:

Base* object = new Derived();
((Derived)object)->member();

The alternative would be to declare member() as a virtual function in the Base class.

- Mike
Quote:Original post by Anonymous Poster
You say it's non-virtual, but a derived class re-implements that method? It has to be virtual. Not that that's why it's freezing, just something that I thought should be pointed out.


not at all true. Implementing a non-virtual member function in a derived class simply "hides" the base class implementation. There is absolutely nothing illegal about it...unclear in most circumstances, certainly...but not wrong. In the same way, you can have a variable in the derived class with the same name as one in the base class and it will hide the base class variable in the derived class.

-me
Her code should work, a the Base class still has the member function defined. Normally, the derived class would hide the Base class's implementation.

In this case, it doesn't happen and the implementation from the base class is just visible and is executed.

As said before, run it through the debugger to see what happens, and after that, post some code.

Toolmaker

See, here's the thing. I already tried declaring function member() as virtual, and it did the same thing. Also, the base class member() function has no implementation. It's not a pure virtual function, it's just empty, like this:

void Base::member(){}

Sorry about not knowing it had to be virtual, I forgot :) I used to be a Java programmer.
if you are still having problems, please just post your code. we can't really help you without it.

However, the best thing to do is to just run it through a debugger and see what goes wrong.

-me

This topic is closed to new replies.

Advertisement