Boost logo

Boost :

From: Eric Weitzman (eweitzman_at_[hidden])
Date: 2000-10-18 20:34:17

I have a class hierarchy of node-type objects which are derived from
Let's say,

        class BaseNode {};
        class DerivedNode : public BaseNode {};

I'm using a vector of pointers to the base class (vector<BaseNode*>) in
various places. I'd like to use reference-counted smart pointers instead.


        typedef shared_ptr<BaseNode> BaseNodePtr;
        typedef shared_ptr<DerivedNode> DerivedNodePtr;

I'd turn the vector into vector<BaseNodePtr>. Now, although I can insert
derived class pointers into the original vector, the smart-pointer versions
of the derived classes do not have any inheritance relationship with the
base class smart pointer and so cannot be inserted. Even if DerivedNodePtr
inherited from BaseNodePtr, I'm inserting objects into the vector, not
pointers, they'd get sliced except for the lucky fact that they're
identically sized template instantiations . None the less, I'd never be able
to recover the type of the DerivedNodePtr without dynamic casting on the

Is there a general solution to replicating the original class hierarchy in
the smart-pointer related types?

I've considered just having one smart-pointer type

        typedef shared_ptr<BaseNode> NodePtr;

and constructing it from various derived-class pointers as needed. But I'd
still have to play games to recover the type of the referent.

- Eric

Boost list run by bdawes at, gregod at, cpdaniel at, john at