C++ C+中的自定义动态指针强制转换实现+;98

C++ C+中的自定义动态指针强制转换实现+;98,c++,templates,linker,c++98,C++,Templates,Linker,C++98,必要解释: 我必须使用C++98,别无选择,是的,我已经向我的芯片供应商要求更新toochain,不,我不会很快得到C++11(哭) 问题: 我已经基于实现了我自己的共享指针(关于为什么我不应该这样做的队列咆哮)。但是,我在实现自己的动态\u指针\u转换函数时遇到困难,或者,至少,我在使实现正确链接时遇到困难 以下是我的代码(在头文件中)实现: #ifndef __SMART_POINTER_HEADER__ #define __SMART_POINTER_HEADER__ class RC

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

问题: 我已经基于实现了我自己的共享指针(关于为什么我不应该这样做的队列咆哮)。但是,我在实现自己的动态\u指针\u转换函数时遇到困难,或者,至少,我在使实现正确链接时遇到困难

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

#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
\ifndef\u智能指针\u头__
#定义\uu智能\u指针\u标题__
RC类
{
私人:
int count;//引用计数
公众:
void AddRef()
{
//增加引用计数
__同步\u添加\u和\u获取(&count,1);
}
int Release()
{
//减少引用计数并
//返回引用计数。
返回uuu sync_usub_u和u fetch(&count,1);
}
};
模板类智能指针
{
私人:
T*pData;//指针
RC*reference;//引用计数
公众:
SmartPointer():pData(0),引用(0)
{
//创建新引用
reference=newrc();
//增加引用计数
reference->AddRef();
}
智能指针(T*pValue):pData(pValue),引用(0)
{
//创建新引用
reference=newrc();
//增加引用计数
reference->AddRef();
}
智能指针(常量智能指针和sp):pData(sp.pData)、参考(sp.reference)
{
//复制构造函数
//复制数据和引用指针
//并增加引用计数
reference->AddRef();
}
~SmartPointer()
{
//析构函数
//减少引用计数
//如果引用变为零,则删除数据
如果(参考->发布()==0)
{
删除pData;
删除引用;
}
}
T&运算符*()
{
返回*pData;
}
T*运算符->()
{
返回pData;
}
智能指针和运算符=(常量智能指针和sp)
{
//赋值运算符
if(this!=&sp)//避免自分配
{
//减少旧的引用计数
//如果引用变为零,则删除旧数据
如果(参考->发布()==0)
{
删除pData;
删除引用;
}
//复制数据和引用指针
//并增加引用计数
pData=sp.pData;
参考=sp.reference;
reference->AddRef();
}
归还*这个;
}
布尔运算符!()常量
{
返回(NULL!=引用);
}
布尔运算符==(常量智能指针和其他)
{
返回(reference==other.reference);
}
布尔运算符==(无效*其他)
{
返回(参考==其他);
}
布尔运算符!=(常量智能指针和其他)
{
返回(reference!=other.reference);
}
布尔运算符!=(无效*其他)
{
返回(参考!=其他);
}
模板朋友智能指针动态\u智能\u指针\u转换(常量智能指针和sp);
};
模板智能指针动态\u智能\u指针\u转换(常量智能指针和sp)
{
智能指针新_sp;
new_sp.pData=动态_cast(sp.pData);
如果(空!=新的\u sp.pData)
{
删除新的sp.pData;
删除新的sp参考;
sp.reference->addRef();
new_sp.pData=sp.pData;
新sp.reference=sp.reference;
}
返回新的_sp;
}
#恩迪夫
如果您发现SmartPointer中存在任何问题,请告诉我,我基本上是从中复制的,并添加了所需的缺少的函数

我的问题是,当我调用dynamic_smart_pointer_cast时,使用以下命令:

dynamic_smart_pointer_cast< BaseClass >( DerivedClassInstance )
dynamic\u smart\u pointer\u cast(DerivedClassInstance)
链接器输出:

undefined reference to `SmartPointer<BaseClass> dynamic_smart_pointer_cast<BaseClass, DerivedClass>(SmartPointer<DerivedClass> const&)'
SmartPointer dynamic\u smart\u pointer\u cast(SmartPointer const&)的未定义引用
我已将模板函数的定义放在包含的头文件中,因此我不确定为什么会出现此链接器错误。有人知道我的问题是什么吗

另外,请随时查看我的动态\u智能\u指针\u强制转换代码,因为我确信它可以改进,并且可能存在问题。

在:

dynamic_smart_pointer_cast ( const SmartPointer<U *>& sp )
dynamic\u smart\u pointer\u cast(const smart pointer&sp)
在下列情况下,去除

中的*:

dynamic_smart_pointer_cast ( const SmartPointer<U *>& sp )
dynamic\u smart\u pointer\u cast(const smart pointer&sp)

去掉

中的*我认为您可以通过LLVM或其他方式将C++1x编译成纯C。任何带有双下划线的标识符都是为标准库保留的。你不太可能会因为违反这条规则而感到刺痛,但当你这么做的时候,神圣蓝精灵的错误是很奇怪的。这里有更多细节:我知道user4581301,我写的代码库非常旧,所有的头保护都使用了双下划线,这可能是我们应该改变的,但需要在大约300个5年以上的文件中删除它。在这一点上,我们坚持使用旧的格式来保持一致性。我认为您可以通过LLVM或其他方式将C++1x编译成纯C。任何带有双下划线的标识符都是为标准库保留的。你不太可能会因为违反这条规则而感到刺痛,但当你这么做的时候,神圣蓝精灵的错误是很奇怪的。这里有更多的细节:我知道user4581301,我写的代码库非常简单