首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >入侵SmartPointer的实现

入侵SmartPointer的实现
EN

Code Review用户
提问于 2017-09-08 01:28:05
回答 1查看 162关注 0票数 4

下面是我的SmartPointer实现。它是侵扰性的和微小的。我该如何改进呢?

例如,constexprnonexcepttry-catch-throw等。

smartpointer.h

代码语言:javascript
复制
/* 
* file name : smartpointer.h
*/
#ifndef __SMARTPOINTER_H__
#define __SMARTPOINTER_H__

#define COMPARE(_op_)                                           \
bool operator _op_ (const SmartPointer& o) const {              \
    return mPointer _op_ o.mPointer;                            \
}                                                               \
bool operator _op_ (const T* o) const {                         \
    return mPointer _op_ o;                                     \
} 

template <typename T> 
class SmartPointer {
public:
    SmartPointer():mPointer(NULL) {std::cout <<"Create null smart pointer."<< std::endl;}   
    SmartPointer(T *p):mPointer(p) {
        std::cout <<"Create smart pointer at "<<static_cast<const void*>(p)<<std::endl;
        if (mPointer) mPointer->incRefCount();
    }     
    ~SmartPointer(){
        std::cout << "Release smart pointer at "<<static_cast<const void*>(mPointer)<<std::endl;
        if (mPointer && mPointer->decRefCount()==0) delete mPointer;
    }
    SmartPointer(const SmartPointer &other):mPointer(other.mPointer) {
        std::cout <<"Copy smart pointer at "<<static_cast<const void*>(other.mPointer)<<std::endl;
       if(mPointer) mPointer->incRefCount();
    }             
   SmartPointer &operator = (const SmartPointer &other) {
       T *temp(other.mPointer);
       if (temp) temp->incRefCount();
       if (mPointer && mPointer->decRefCount()==0) delete mPointer;
       mPointer = temp;  
       return *this;
   } 

    T&  operator* () const {return *mPointer;}
    T* operator-> () const {return mPointer;}


    COMPARE(==);
    COMPARE(!=);
private:
    T *mPointer;
};

class RefBase   
{   
    public:   
        RefBase() : mCount(0){ }   
        void incRefCount(){   
            mCount++;   
        }   
        int decRefCount(){   
            return --mCount;
        }      
        int getRefCount() const {   
            return mCount;   
        }  

        virtual ~RefBase(){};
    private:   
        int mCount;   
};   
#endif // __SMARTPOINTER_H__

sptestcase4.cpp (测试用例)

代码语言:javascript
复制
/* 
* file name : sptestcase4.cpp
*/

#include <iostream>
#include "smartpointer.h"

class SomeClass: public RefBase{
public:
    SomeClass(){std::cout << "SomeClass default constructor !"<<std::endl;}
    ~SomeClass(){std::cout << "SomeClass deconstructor !"<<std::endl;}
    void func(){std::cout << "SomeClass func()" <<std::endl;}
};

class OtherClass: public RefBase{
public:
    OtherClass(){std::cout << "OtherClass default constructor !"<<std::endl;}
    ~OtherClass(){std::cout << "OtherClass deconstructor !"<<std::endl;}
    void foo(){std::cout << "OtherClass foo()" <<std::endl;}
};


void testcase4_1(void)
{
    std::cout << "=======testcase4_1=========" <<std::endl;
    SmartPointer<SomeClass> spsomeclass = new SomeClass();
    (*spsomeclass).func();
    spsomeclass->func();
    std::cout << "==========================" <<std::endl;
}

void testcase4_2(void)
{
    std::cout << "=======testcase4_2=========" <<std::endl;
    SomeClass *psomeclass = new SomeClass();
    SmartPointer<SomeClass> spsomeclass = psomeclass;

    SmartPointer<OtherClass> spotherclass = new OtherClass();
    SmartPointer<OtherClass> spotherclass2 = spotherclass;

    if (spsomeclass == NULL) std::cout<< "spsomeclass is NULL pointer" << std::endl;
    if (spotherclass != NULL) std::cout<< "spotherclass is not NULL pointer" << std::endl;
    if (spsomeclass == psomeclass)  
       std::cout<< "spsomeclass and psomeclass are same pointer" << std::endl;
    if (spsomeclass != psomeclass)  
       std::cout<< "spsomeclass and psomeclass are not same pointer" << std::endl;
//     if (spsomeclass != spotherclass) // ERROR !
//        std::cout<< "spsomeclass and spotherclass are not same pointer" << std::endl;
//     if (spsomeclass == spotherclass) // ERROR !
//        std::cout<< "spsomeclass and spotherclass are same pointer" << std::endl;
    if (spotherclass == spotherclass2) std::cout<< "spotherclass and spotherclass2 are same pointer" << std::endl;
    if (spotherclass != spotherclass2) std::cout<< "spotherclass and spotherclass2 are not same pointer" << std::endl;    
    std::cout << "==========================" <<std::endl;

}

int main(void)
{
    testcase4_1();
    testcase4_2();
    return 0;
}
EN

回答 1

Code Review用户

发布于 2017-09-08 01:45:40

通过使用以下划线开头的标识符,您将调用未定义的行为。只要去掉下划线。

在头文件的末尾,您应该取消对比较宏的定义。或者至少考虑完全删除宏。

&的位置不一致。

您的头文件必须自己编译,即使它是在<iostream>之前包含的。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/175097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档