Skip to main content
gamedev.net
🔒 Locked

Virtual Function Question

Started by WayfarerX Dec 21, 2001 at 12:01 PM 4 replies 700+ views
Original Post
WayfarerX
WayfarerX
A quick example to start things off:
  
class CMyClass
{
public:
  virtual void vFunc ();
  void func();
};
  
What I''m wanting to know is if there is a way to force the pointer to vFunc in the vtable to point to func(). I tried:
virtual void vFunc () = func;
 
and of course that doesn''t work. Is there a way to do this? I''m using MSVC++6. Thanks in advance. "So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable." - Tool ------------------------------
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
jenova
jenova
not sure what you''re trying to do! but if you are trying to setup dynamic function calling you can try this.


// declare a variable to a function type. i.e.
int (* funcptr)(void);

int return1(void)
{
return (1);
}


// now you can assign "funcptr" to any function of the same type.
funcptr = return1;


// now you can call "return1" through "funcptr".
funcptr();


hope this helps.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
WayfarerX
WayfarerX
Close, I''d thought of that. The specific case I''m having, though, is that CMyClass extends a pure virtual base class IMyClass that defines vFunc(), so I don''t think function pointers will work.

"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Warden
Warden
I got lost at what you''re trying to do? You have a pure virtual interface and a class that inherits that interface, and a function (vFunc) which implements one of the interface functions. Now you want another function (func) to instead handle the implementation? Um... okay?

Anyway, don''t think you can phyisically manipulate the vtable, though I could be wrong. (I''ve never tried and I really don''t want to! ). However, you can inline your fall to func. Example:

CMyClass
{
public:
virtual void vFunc() { func(); };
void func();
}
-Warden
null_pointer
null_pointer
Why not simply override the virtual function call?

#include <iostream>

using std::cout;

class interface
{
public:
virtual void function() = 0;
};

class implementation : public interface
{
public:
virtual void function() { cout << "implementation::function() called"; }
}

int main()
{
interface* p = new implementation;
p->function();
return 0;
}


Do you need to assign these at run-time? You have one of two options:

1. Give each class its own implementation and assign the address of an instance of one of the implementation classes to an interface pointer, depending on which function you want to call.

2. Define a member function pointer and assign the address of the member function based on which function you want to call.

Which one is best suited to your particular problem?
WayfarerX
WayfarerX
Well, thanks for the help guys. This isn't so much of a problem as it was just a curiosity. Let me give you a better example of what I am doing.

I'm working on a small, self-contained game library that presents itself to the client application via a set of pure virtual interfaces. I have an interface that provides miscellaneous system functionality, like returning a time delta.

    
struct ISystem
{
virtual unsigned int getTimeDelta () = 0;
};


This is provided in the interface for completeness, but is primarily used inside the library itself for things like profiling and animation timing. Since it's a small and frequently called function, having the overhead of a virtual function call seems a little wasteful. So I defined my implementation like this (as Warden said):

    
class CSystem : public ISystem
{
public:
virtual unsigned int getTimeDelta ()
{
return impl_getTimeDelta();
}
unsigned int impl_getTimeDelta ();
};


So from inside the library I can call impl_getTimeDelta directly.
So looking at this, I realized that the vtable entry for getTimeDelta pointed to getTimeDelta, which then just forwarded the call onto impl_getTimeDelta. So it sparked my interest as to if it was possible to explicitly declare a separate function for a virtual function to point to.

Obviously this isn't a mission-critical problem, I was just kind of curious. Couldn't find any relevant info in a couple of quick searches or in any of my books, so I figured I'd see if you guys knew anything about it. I'm assuming from your responses that it's not possible, so I'll just leave it as-is.

Thanks again for the help.

"So crucify the ego, before it's far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that's imagined and all conceivable."
- Tool
------------------------------


Edited by - wayfarerx on December 21, 2001 3:14:33 PM
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.