Sign in to follow this  

my computer ain't likin' polymorphism

This topic is 4668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this