shared_ptr成员具有多态性

时间:2017-05-24 07:53:20

标签: c++ c++11 shared-ptr

我有B + Set

这样的代码
template<class _Key>
class BPlusTreeSetNode
{
   public:
.............................
        void setParent(std::shared_ptr<BPlusTreeSetNode>& pNode, int32 nFoundIndex = -1)
        {
            m_pParent =  pNode;
            m_nFoundIndex = nFoundIndex;
            if (pNode.get())
                m_nParent = pNode->addr();
            else
                m_nParent = -1;
        }

        int64 parentAddr() const { return m_nParent; }
        int32 foundIndex() const  { return m_nFoundIndex; }
        void setFoundIndex(int32 nFoundIndex) { m_nFoundIndex = nFoundIndex; }
        std::shared_ptr<BPlusTreeSetNode> parentNodePtr() { return m_pParent.lock(); }
............................
  protected:

    typedef std::weak_ptr<BPlusTreeSetNode> TParentNodePtr;
    TParentNodePtr m_pParent;
    int32 m_nFoundIndex;
    int64 m_nParentAddr;
};

.......................................
template< class _TKey,  class _TNode = BPlusTreeSetNode<_TKey> >
class TBPlusSet
{
................................................
typedef std::shared_ptr<_TNode> TreeNodePtr;
TreeNodePtr m_pRoot;
................................................
TIterator find(const TKey& key)
{
..........................................................................
    int32 nIndex = -1;
    int64 nNextAddr = m_pRoot->lower_bound(comp, key, nIndex);
    TreeNodePtr pParent = m_pRoot;
    for (;;)
    {
        if (nNextAddr == -1)
        {
            break;
        }
        TreeNodePtr pNode = getNode(nNextAddr);
        if (!pNode.get())
        {
            return TIterator(this, TreeNodePtr(), -1);
            break;
        }
        pNode->setParent(pParent, nIndex); // <-- There is error

     .......................................................
    } 
}
};

在B + Set中一切都好。但如果我将B + Set扩展为B + Map:

template<class _Key,class _Value,  class _Transaction>
class BPlusTreeMapNode : public BPlusTreeSetNode<_Key, _Transaction>
{
   public:
..........................................
};

template< class _Key, class _Value, class _TNode = BPlusTreeMapNode<_TKey, _Value> >
class TBPlusMap : public TBPlusSet<_Key, _TNode >
{
..................................
}

我收到错误

错误C2664'void embDB :: BPlusTreeSetNode&lt; _Key&gt; :: setParent(std :: shared_ptr

<_Key> > &,int32)':
 cannot convert argument 1 from 'std::shared_ptr<embDB::BPlusTreeMapNode<int64,int64>>' to 'std::shared_ptr<embDB::BPlusTreeSetNode<_TKey> &

我认为在这个架构中,使用std :: enable_shared_from_this的唯一解决方案,也许还有其他解决方案?

0 个答案:

没有答案