Archived

This topic is now archived and is closed to further replies.

Virtual Fighter II

This topic is 6366 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

Kinda long-winded, but here goes: It seems as though I''ve hit another obstacle in my game *gasps around the table*. Only this time, I know what the problem is. I just need to know how to work around it. I''ve got a class, let''s call it CClass. It has a virtual function, let''s call it MyVirtualFunction() (notice the innovative names I''m using). Surprisingly enough, I do know how to declare virtual functions and what they are used for, so don''t worry about that. Now, I have a class derived from CClass, let''s call it CChildClass. It overrides the virtual function MyVirtualFunction(), with its own virtual function of the same name. The parameter list is the same. All right, if you''re with me so far, I now have a CClass pointer, shall we call it *MyPointer. Now, during the function that MyPointer is declared in, one of the following will happen. Either:
MyPointer = new CClass;
 
or
MyPointer = new CChildClass;
 
This works great, as this is one of the beauties of pointers. However, in this same function, I call MyPointer->MyVirtualFunction(). If MyPointer points to a CClass object, I want it to call CClass::MyVirtualPointer(). However, if it points to a CChildClass object, I want it to call CChildClass::MyVirtualPointer(). Which I thought should work because the function is virtual. This, as it happens, is my own undoing. Because *MyPointer is a pointer of type CClass, it only calls CClass::MyVirtualFunction() regardless of wether it is pointing to a CClass object or a CChildClass object. So what do I do? Do I make MyVirtualFunction() a normal member function instead of virtual in both classes? Just one? Do I leave it virtual, but use some other pointer? Do I just call it differently? Your help is appreciated in advance, as always. The_Minister 1C3-D3M0N Interactive

Share this post


Link to post
Share on other sites
That doesn''t make sense. If the CChildClass overrides the virtual function in CClass, the method called should depend on the type assigned to the pointer.

CClass * MyPointer = new CClass;

MyPointer->MyVirtualFunction(); // invokes CClass::MyVirtualFunction

CClass * MyPointer = new CChildClass;

MyPointer->MyVirtualFunction(); // invokes CChildClass::MyVirtualFunction

So I don''t understand why this doesn''t work for you..?

I can only guess that the CClass function is not declared as virtual, which would result in what you get.

Share this post


Link to post
Share on other sites
I am just as confused as you are, if not more. I am following the flow, and when it should go to the derived class''s function, it goes to the base class''s... maybe the problem lies where I am allocating the pointer memory. I''ll inspect it now, but I doubt that''s where the problem is, because the only difference between the version that worked and the version that didn''t is that virtual function.

The_Minister
1C3-D3M0N Interactive

Share this post


Link to post
Share on other sites