473,414 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

virtual vs. function pointer

41
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:
Expand|Select|Wrap|Line Numbers
  1. class someClass : public someOtherClass
  2. {
  3. public:
  4.   virtual void f(); // this function is overloading f() in someOtherClass
  5. }
  6.  
Expand|Select|Wrap|Line Numbers
  1. class isThisBetter
  2. {
  3. public:
  4.   fPtr f;
  5. }
  6.  
A pointer is assigned to f at some point after creation.
Jul 2 '08 #1
6 4130
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. class isThisBetter
  2. {
  3. public:
  4.     int (*f)(int,int);
  5.   };
  6.  
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.
Jul 2 '08 #2
TamusJRoyce
110 100+
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...
Jul 7 '08 #3
JonLT
41
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:
Expand|Select|Wrap|Line Numbers
  1. class baseObject
  2. {
  3. public:
  4.    float intersectionTest(Ray r)
  5.    {
  6.       iTest(r);
  7.    }
  8. protected:
  9.    fPtr iTest;
  10. };
  11.  
Jul 8 '08 #4
TamusJRoyce
110 100+
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)
Jul 8 '08 #5
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:
Expand|Select|Wrap|Line Numbers
  1. class baseObject
  2. {
  3. public:
  4.    float intersectionTest(Ray r)
  5.    {
  6.       iTest(r);
  7.    }
  8. protected:
  9.    fPtr iTest;
  10. };
  11.  
just a - possibly obvious - note:
"float intersectionTest(const Ray& r)" may perform better.
Jul 8 '08 #6
...
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.
Jul 8 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
7
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
7
by: ashishnh33 | last post by:
i want to know about the concept behind the virtual functions.
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.