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<C>  vc;           // this one...
vector<C*>  vpc;         // ...or this one
  • Any other option?

Thanks a lot!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.