Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

virtual vs. function pointer

Question posted by: JonLT (Member) on July 2nd, 2008 11:11 PM
Hi

I was just wandering what the difference between a overloaded virtual function and a function pointer member variable is?
Im looking for the best execution time.
Heres an example:
Code: ( text )
  1. class someClass : public someOtherClass
  2. {
  3. public:
  4.   virtual void f(); // this function is overloading f() in someOtherClass
  5. }

Code: ( text )
  1. class isThisBetter
  2. {
  3. public:
  4.   fPtr f;
  5. }


A pointer is assigned to f at some point after creation.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
weaknessforcats's Avatar
weaknessforcats
Moderator
4,656 Posts
July 2nd, 2008
11:29 PM
#2

Re: virtual vs. function pointer
Quote:
Originally Posted by JonLT
class isThisBetter
{
public:
fPtr f;
}


Actually, this is worse because you have a public data member accessible (and changeable) by any function in the program. When this gets screwed up, you will have a whole lotta suspects.

There is no similarity between a function pointer and a virtual function.

A function pointer is a pointer to a function of the kind used to define the function pointer. That means the f public data member in your example won't compile because the compiler does not know what sorts of arguments and return type (if any) are required. You need to tell the compiler what to do. Below is your f defined as a pointer to a function that takes two int arguments and returns an int:

Code: ( text )
  1. class isThisBetter
  2. {
  3. public:
  4.     int (*f)(int,int);
  5.   };


This means f can contain the address of any function so long as it has two int arguments and returns an int.


The virtual keyword on a member function tells the compiler that if you use a pointer or reference to a base class object and there is a choice between calling the base class function or the derived class member function, the derived member function is to be called.

Remove the virtual keyword and the compiler is told that if there is a choice between the base class function and the derived function, use the base function.

Therefore, the virtual keyword allows derived class function to be called using a base class object pointer or reference. And this is how you do object-oriented coding in C++.

While it is true the addresses of these member functions are used in the class virtual function table, the table does not contain function pointers as defined in C++.

You use a function pointer when you and not your object needs to make a run-time decision as to what function to call.

Reply
TamusJRoyce's Avatar
TamusJRoyce
Member
51 Posts
July 7th, 2008
12:24 AM
#3

Re: virtual vs. function pointer
From what I've learned dealing with COM and DirectX, using virtual methods does take a hit on both performance and overhead vs. using a classic C style callback function (function pointer). This is due to the vthunk table having to be referenced for each access to the virtual function you are overloading.

But unless you are doing 1000 loops on a time critical pixel-to-screen operation, performance probably won't be hit almost at all using a virtual function vs. a C style callback. Heck. DirectX and COM are based on virtual functions (look up how to do iunkown interface if you're curious).

But using virtual inefficiently is kind of why Java runs a lot slower (compiled natively) than C/C++ (but it helps incredibly with garbage collection), so don't go wild over using it if you are concerned about efficiency (which means you're a good programmer : ).

Note: Personally, I would declare an inline method (inline defaultly if body of function is left within the class) and then call that method inside the virtual method. Then avoid using the virtual function unless at all nessesary (i.e. when you have to call it directly from a base class).
The inline will allow the compiler to determine whether or not to inline it based on the size of the function body and the optimization parameters you send it. Then test the various optimizations and record the running time & size of the executable, choosing how much performance vs. size (both in memory and on disk) you need.

Feel free for anyone to comment on me here. I don't always compose my ideas correctly, nor am I right that much either :-b...

Last edited by TamusJRoyce : July 7th, 2008 at 12:39 AM. Reason: Feel free to comment
Reply
JonLT's Avatar
JonLT
Member
41 Posts
July 8th, 2008
09:45 AM
#4

Re: virtual vs. function pointer
Thank you very much. I am actually in the process of making a raytracer, and i'd like it to have a reasonable frame rate, so every little improvement is welcome, and from what you are saying it sounds like i need to avoid virtual function calls in the main loop.

The reason i was using them in the first place, was to be able to do intersection tests with different objects by calling an intersectionTest() function on some baseObject class. But i guess i'll just scrap the different objects and only use triangles.
Or perhaps a function pointer in the baseObject... some thing like this:
Code: ( text )
  1. class baseObject
  2. {
  3. public:
  4.    float intersectionTest(Ray r)
  5.    {
  6.       iTest(r);
  7.    }
  8. protected:
  9.    fPtr iTest;
  10. };

Reply
TamusJRoyce's Avatar
TamusJRoyce
Member
51 Posts
July 8th, 2008
07:55 PM
#5

Re: virtual vs. function pointer
I'm glad I could help.

Yeah. For ray-tracing and just using the function as a wrapper as it look like you are doing, I would try to call it inline without a pointer, if I could.

And using the keyword register for small parameters would be of utmost importance (placed appropriately, since using them everywhere can slow down your program), too.

It's against the rules of the forum to post links to outside the forum, but pm me directly if you have any interesting tutorials and such about ray-tracing and you have some time (it kinda parallels 3D engines which I'm slowly learning the math on)

Reply
romanize's Avatar
romanize
Newbie
5 Posts
July 8th, 2008
10:48 PM
#6

Re: virtual vs. function pointer
Quote:
Originally Posted by JonLT
Thank you very much. I am actually in the process of making a raytracer, and i'd like it to have a reasonable frame rate, so every little improvement is welcome, and from what you are saying it sounds like i need to avoid virtual function calls in the main loop.

The reason i was using them in the first place, was to be able to do intersection tests with different objects by calling an intersectionTest() function on some baseObject class. But i guess i'll just scrap the different objects and only use triangles.
Or perhaps a function pointer in the baseObject... some thing like this:
Code: ( text )
  1. class baseObject
  2. {
  3. public:
  4.    float intersectionTest(Ray r)
  5.    {
  6.       iTest(r);
  7.    }
  8. protected:
  9.    fPtr iTest;
  10. };


just a - possibly obvious - note:
"float intersectionTest(const Ray& r)" may perform better.

Reply
romanize's Avatar
romanize
Newbie
5 Posts
July 8th, 2008
10:54 PM
#7

Re: virtual vs. function pointer
Quote:
Originally Posted by TamusJRoyce
...
And using the keyword register for small parameters would be of utmost importance (placed appropriately, since using them everywhere can slow down your program), too.
...


Similarly, be aware of the many optimizations the hardware takes care of. If you have, say, 10 different objects to intersect with, the referenced locations may end up very close to the processor after several calls, such that there is no - or little - memory overhead.

As for the keyword register, I'd trust the compiler to do the job.

Correct, if I'm wrong, please.

The best thing I to do some benchmarks, I guess.

Reply
Reply
Not the answer you were looking for? Post your question . . .
184,038 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top C / C++ Forum Contributors