#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;
}
c++ 智能指针
最新推荐文章于 2025-05-11 16:47:53 发布