用模板实现引用计数


#ifndef T_NO_ANSI_CASTS 
# define T_REINTERPRET_CAST(type,pointer) reinterpret_cast< type >(pointer)
# define T_STATIC_CAST(type,pointer) static_cast< type >(pointer)
# define T_CONST_CAST(type,pointer) const_cast< type >(pointer)
#else
# define RW_REINTERPRET_CAST(type,pointer) (type)pointer
# define RW_STATIC_CAST(type,pointer) (type)pointer
# define RW_CONST_CAST(type,pointer) (type)pointer
#endif /* T_NO_ANSI_CASTS */

class CReference 
{
public:
    CReference(){}
    virtual ~CReference() {}

    void addReference()
    {
        refCount_ ++;
    }

    int removeReference()
    {
        return refCount_ --;
    }

    int references() const 
    {
        return refCount_;
    }

private:
    volatile int  refCount_;
    //unimplemented:
    // If you ever implement these, use rwAtomicExchange to copy the classes
    CReference(const CReference&);
    CReference& operator=(const CReference&);
};



你可能感兴趣的:(模板)