【问题标题】:Custom dynamic_pointer_cast implementation in C++98C++98 中的自定义 dynamic_pointer_cast 实现
【发布时间】:2018-01-13 00:00:40
【问题描述】:

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

问题: 我已经基于this 实现了我自己的共享指针(排队抱怨我为什么不应该这样做)。但是,我在实现自己的 dynamic_pointer_cast 函数时遇到了麻烦,或者,至少,我无法让我的实现正确链接。

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

#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 )

链接器输出:

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

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

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

【问题讨论】:

  • 我认为您可以通过 LLVM 或其他方式将 C++1x 编译为纯 C。 stackoverflow.com/questions/1833484/…
  • 任何带有双下划线的标识符都是为标准库保留的。你不太可能感受到违反这条规则的刺痛,但是当你这样做时,神圣的蓝精灵就是奇怪的错误。更多细节在这里:stackoverflow.com/questions/228783/…
  • 我知道 user4581301,我编写的代码库非常旧,所有标题保护都使用双下划线,可能我们应该更改一些内容,但需要在大约 300 个文件中删除它,即 5 + 岁。在这一点上,我们坚持使用旧格式以保持一致性。

标签: c++ templates linker c++98


【解决方案1】:

在:

dynamic_smart_pointer_cast ( const SmartPointer<U *>& sp )

去掉

里面的*

【讨论】:

  • 谢谢,这就是问题所在!我讨厌一个字符的错误,很难发现,至少编译器抓住了它。
猜你喜欢
  • 2022-06-13
  • 1970-01-01
  • 2020-08-16
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
相关资源
最近更新 更多