|
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
BaseNode.
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.
Given
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
referent.
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.
Thanks,
- Eric
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk