Re: How make polymorphism optional?
Daniel T. wrote:
Litvinov Sergey <slitvinov@gmail.com> wrote:
My problem is the following one.
I have a huge number of objects of base class:
class Base {
public:
virtual void
method();
}
And a derived class:
class Derived : publcic Base {
public:
virtual void
method();
}
Sometime I have no objects of Derived class and in those cases
I would like to get rid of polymorphism overhead. (speed is crucial
for me).
If speed is crucial, you should profile and find out where best to
spend your efforts, this is unlikely to be one of those places.
In any case:
vector<Base> vec(1); // make the vector as big as the
// number of objects you have
vec[0].method(); // no virtual overhead.
Are you sure about "no virtual overhead"? The vector::operator[]
returns a *reference*, not an object. With a reference a call to
a virtual function *should* go through the mechanism resolving all
the virtual function calls to the final overrider. It's not
statically resolved to Base::method, if that's what you implied.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask