954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Virtual functions, function objects, or what?

I have a huge set of small objects that work differently, but with a common interface.
They have to stay in the same container.
I cannot use templates, because I need run-time polymorphism.

*** Run time efficiency is critical ***

Can anybody tell me which of these options is the most efficient one (in both time and space)?

- switch
- virtual functions
- function objects:

class C {
// interface...
FO *kernel; // function object
};
vector vc; // this one...
vector vpc; // ...or this one

- Any other option?

Thanks a lot!

Pietro (pietromele@yahoo.com)
:lol:

pietromele
Newbie Poster
1 post since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

The second option (with pointer) will be most efficient and fastest because the first option makes a copy of the object before putting it into the vector while the second one does not. And depending on the contents of class C the first option can be very very slow.

The flip side is during destruction of the vector -- you must destroy (delete) each object in the vector yourself, the vector will do that with the first option but not the second.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You