c++ 智能指针

#include <iostream>

enum shape_t {
    Rectangle,
    Triangle,
    Circle
};

class shape {
  public:
    virtual void debug_string() const = 0;
    virtual ~shape(){}
};

class rectange final : public shape {
  public:
    void debug_string() const override {
        std::cout << "Rectange..." << std::endl;
    }

    ~rectange() {
        std::cout << "Rectange destructor" << std::endl;
    }
};

class triangle final : public shape {
  public:
    void debug_string() const override {
        std::cout << "Triangle..." << std::endl;
    }

    ~triangle() {
        std::cout << "Triangle destructor" << std::endl;
    }
};

class circle final : public shape {
  public:
    void debug_string() const override {
        std::cout << "Circle..." << std::endl;
    }

    ~circle() {
        std::cout << "Circle destructor" << std::endl;
    }
};

// ------------------------- break line ------------------------

shape* create_shape_native(shape_t type) {
    switch(type) {
        case Rectangle : return new rectange();
        case Triangle: return new triangle();
        case Circle: return new circle();
        default: return nullptr;
    }
}

void test1_native_rall() {
    shape* sh = create_shape_native(Rectangle);
    if (sh != nullptr) {
        sh->debug_string();
    }

    delete sh;
}

template<typename T>
class shape_wrapper {
  public:
    shape_wrapper(T* ptr = nullptr) : ptr_(ptr) {}
    ~shape_wrapper() {
        delete ptr_;
        // std::cout << "call shape_wrapper destructor" << std::endl;
    }
    T* get() { return ptr_; }
    T& operator*() { return *ptr_; }
    T* operator->() { return ptr_; }
    operator bool() const { return ptr_; }
    void operator()() const { ptr_->debug_string(); }

#ifdef UNABLECOPY
    shape_wrapper(const shape_wrapper<T>& other) = delete;
    shape_wrapper<T>& operator==(const shape_wrapper<T>& other) = delete;
#endif

#ifdef AUTOPTR
    shape_wrapper(shape_wrapper<T>& other) {
        ptr_ = other.get();
        other.ptr_ = nullptr;
    }

    shape_wrapper<T>& operator=(shape_wrapper<T>& other) {
        if (this == &other) return *this;
        T* tmp_ptr = ptr_;
        ptr_ = other.ptr_;
        other.ptr_ = nullptr;
        delete tmp_ptr;
        return *this;
    }
#endif

#ifdef UNIQUEPTR
    shape_wrapper(shape_wrapper<T>&& other) {
        ptr_ = other.get();
        other.ptr_ = nullptr;
    }

    shape_wrapper<T>& operator=(shape_wrapper<T> other) {
        if (this == &other) return *this;
        T* tmp_ptr = ptr_;
        ptr_ = other.ptr_;
        other.ptr_ = nullptr;
        delete tmp_ptr;
        return *this;
    }
#endif

  private:
    T* ptr_;
};

void test2_shape_wrapper_rall() {
    shape_wrapper<shape> sp(create_shape_native(Rectangle));
    if (sp) {
        // sp->debug_string();
        // (*sp).debug_string();
        // sp();
    }

#ifdef AUTOPTR
    shape_wrapper<shape> spp1(sp);
    shape_wrapper<shape> spp2(create_shape_native(Triangle));
    if (sp) {
        std::cout << "sp" << std::endl;
    }
    if (spp1) {
        spp2();
        spp2 = spp1;
        spp2();
    }
    if (spp1) {
        std::cout << "spp1" << std::endl;
    }
#endif

#ifdef UNIQUEPTR
    shape_wrapper<shape> spp1(create_shape_native(Rectangle));
    shape_wrapper<shape> spp2(create_shape_native(Triangle));
    if (sp) {
        std::cout << "sp" << std::endl;
    }
    if (spp1) {
        spp2();
        spp2 = std::move(spp1);
        spp2();
    }
    if (spp1) {
        std::cout << "spp1" << std::endl;
    }
#endif
}

int main(void) {
    test2_shape_wrapper_rall();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值