Boost logo

Boost :

From: Dave Harris (brangdon_at_[hidden])
Date: 2005-10-27 13:19:51


In-Reply-To: <djpuvk$q3d$1_at_[hidden]>
simon_at_[hidden] (Simon Buchan) wrote (abridged):
> > Generally when I've wished for that I've also wished for a few other
> > virtual functions. For example, a hook which gets called whenever an
> > item is added to the vector [...]
>
> That's what your class would add, presumably.

Yes, but as I understand it, I can't usefully add them by inheriting from
the proposed class. This is because, eg, virtual_vector::push_back is not
virtual. Users could do:

    my_vec<int> *p1 = new my_vec;
    p1->push_back( 0 ); // OK, uses my hook.
    virtual_vector *p2 = p1;
    p2->push_back( 0 ); // Silent error, does not use my hook.

In my view to be useful a virtual_vector would need lots of virtual
methods, not just the destructor.

> But when you *do* want to write a vector (or whatever) interface
> compliant class, it's a bloody pain to do it for each one you do.

I can only report my experience, which is that I've never wanted to do
that.

> His impl has the benefit that you just publicly inherit from this class,
> and add the extra stuff in, overriding any methods that you want to do
> differently. (I assume the forwarders will be virtual?)

I didn't think he did intend that the forwarders be virtual. If it is,
then I withdraw my objection. And I am curious as to whether he has:

    void virtual_vector::clear() {
        m_vector.clear();
    }

or:
    void virtual_vector::clear() {
        erase( begin(), end() );
    }

That is, whether I have to override both erase and clear or just erase.
And whether things like begin() and end() are also virtual, and whether
the iterators they return have virtual methods too. It sounds like it
could be a performance hit.

> > In general inheriting from anything in the std library has the
> > drawback that it injects unknown junk names into your class.
> > Inheriting from a boost class is hardly better.
>
> Actually, his posted impl doesn't do that.

He isn't doing, but he proposes that users do. The reason for giving
boost::virtual_vector a virtual destructor is so that users can inherit
from it.

-- Dave Harris, Nottingham, UK.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk