C++98 中的自定义 dynamic_pointer_cast 实现

Custom dynamic_pointer_cast implementation in C++98

必要的解释: 我必须使用 C++98,别无选择,是的,我已经向我的芯片供应商询问了更新的 toochain,不,我不会很快得到 C++11(哭)。

问题: 我已经基于 this 实现了我自己的共享指针(关于为什么我不应该这样做的队列咆哮)。但是,我在实现自己的 dynamic_pointer_cast 函数时遇到了问题,或者至少,我在实现 link 时遇到了问题。

这是我的代码(在头文件中)实现:

#ifndef __SMART_POINTER_HEADER__
#define __SMART_POINTER_HEADER__

class RC
{
    private:
        int count; // Reference count

    public:
        void AddRef()
        {
            // Increment the reference count
            __sync_add_and_fetch( &count, 1 );
        }

        int Release()
        {
            // Decrement the reference count and
            // return the reference count.
            return __sync_sub_and_fetch( &count, 1 );
        }
};

template < typename T > class SmartPointer
{
    private:
        T* pData;       // pointer
        RC* reference; // Reference count

    public:
        SmartPointer() : pData( 0 ), reference( 0 )
        {
            // Create a new reference
            reference = new RC();
            // Increment the reference count
            reference->AddRef();
        }

        SmartPointer( T* pValue ) : pData( pValue ), reference( 0 )
        {
            // Create a new reference
            reference = new RC();
            // Increment the reference count
            reference->AddRef();
        }

        SmartPointer( const SmartPointer<T>& sp ) : pData( sp.pData ), reference( sp.reference )
        {
            // Copy constructor
            // Copy the data and reference pointer
            // and increment the reference count
            reference->AddRef();
        }

        ~SmartPointer()
        {
            // Destructor
            // Decrement the reference count
            // if reference become zero delete the data
            if ( reference->Release() == 0 )
            {
                delete pData;
                delete reference;
            }
        }

        T& operator* ()
        {
            return *pData;
        }

        T* operator-> ()
        {
            return pData;
        }

        SmartPointer<T>& operator = ( const SmartPointer<T>& sp )
        {
            // Assignment operator
            if ( this != &sp ) // Avoid self assignment
            {
                // Decrement the old reference count
                // if reference become zero delete the old data
                if ( reference->Release() == 0 )
                {
                    delete pData;
                    delete reference;
                }

                // Copy the data and reference pointer
                // and increment the reference count
                pData = sp.pData;
                reference = sp.reference;
                reference->AddRef();
            }

            return *this;
        }

        bool operator ! () const
        {
            return ( NULL != reference );
        }

        bool operator == ( const SmartPointer<T>& other )
        {
            return( reference == other.reference );
        }

        bool operator == ( void * other )
        {
            return( reference == other );
        }

        bool operator != ( const SmartPointer<T>& other )
        {
            return( reference != other.reference );
        }

        bool operator != ( void * other )
        {
            return( reference != other );
        }

        template <class Y, class U> friend SmartPointer<Y> dynamic_smart_pointer_cast( const SmartPointer<U>& sp );
};

template <class T, class U> SmartPointer<T> dynamic_smart_pointer_cast ( const SmartPointer<U *>& sp )
{
    SmartPointer<T> new_sp;
    new_sp.pData = dynamic_cast<T*>( sp.pData );

    if( NULL != new_sp.pData )
    {
        delete new_sp.pData;
        delete new_sp.reference;
        sp.reference->addRef();
        new_sp.pData = sp.pData;
        new_sp.reference = sp.reference;
    }

    return new_sp;
}

#endif

如果您在 SmartPointer 中发现任何问题,请告诉我,我基本上是从 here 复制它并添加了我需要的缺失功能。

我的问题是,当我调用 dynamic_smart_pointer_cast 时:

dynamic_smart_pointer_cast< BaseClass >( DerivedClassInstance )

linker 输出:

undefined reference to `SmartPointer<BaseClass> dynamic_smart_pointer_cast<BaseClass, DerivedClass>(SmartPointer<DerivedClass> const&)'

我已将模板函数的定义放在包含的头文件中,所以我不确定为什么会收到此 linker 错误。有谁知道我的问题可能是什么?

也可以随时查看我的 dynamic_smart_pointer_cast 代码,因为我相当确定它可以改进并且可能存在问题。

在:

dynamic_smart_pointer_cast ( const SmartPointer<U *>& sp )

去掉 <>

里面的 *