effective c++避免返回handles指向对象内部成分(2)

class Point
{
public:
    Point(int _x,int _y):x(_x),y(_y){}
    void setX(int _x){x = _x;}
    void setY(int _y){y = _y;}
    int getX()const{return x;}
    int getY()const{return y;}
private:
    int x,y;
};
class RectData
{
public:
    RectData(Point _ulhc,Point _lrhc):ulhc(_ulhc),lrhc(_lrhc){}
    Point ulhc,lrhc;
};
class Rectangle
{
public:
    Rectangle(Point ul,Point lr):
        pData(new RectData(ul,lr)){}
    const Point& upperLeft()const{return pData -> ulhc;}
    const Point& lowerRight()const{return pData -> lrhc;}
private:
    shared_ptr<RectData>pData;
};
const Rectangle foo(const Rectangle & r){return r;}
int main()
{
    const Rectangle rec(Point(0,0),Point(1,1));
    const Point* p = &(foo(rec).upperLeft());
    //foo函数返回的是一个新的,暂时的rectangle对象,对象没有名字,我们叫他temp
    //语句执行完,temp被销毁,间接导致temo内的points析构
    //导致p指针指向一个不再存在的对象
}

后面又补充了一些shared_ptr的有关学习
但是有些不清楚,为何不需要为Base声明一个virtual析构函数
下面都是从cppreference上面找的
总结:初学一个语法,看文档还是不错的选择,如果想深入学习的话可以上网在博客上找一找

#include <iostream>
#include <memory>
#include <thread>
#include <chrono>
#include <mutex>

struct Base
{
    Base() { std::cout << "  Base::Base()\n"; }
    // Note: non-virtual destructor is OK here
    ///why??
    ~Base() { std::cout << "  Base::~Base()\n"; }
};
struct Derived: public Base
{
    Derived() { std::cout << "  Derived::Derived()\n"; }
    ~Derived() { std::cout << "  Derived::~Derived()\n"; }
};

void thr(std::shared_ptr<Base> p)
{

    std::shared_ptr<Base> lp = p; // thread-safe, even though the
                                  // shared use_count is incremented
    std::this_thread::sleep_for(std::chrono::seconds(1));
    {
      static std::mutex io_mutex;
      std::lock_guard<std::mutex> lk(io_mutex);
      //When a lock_guard object is created, it attempts to take ownership of the mutex it is given.
      //When control leaves the scope in which the lock_guard object was created,
      //the lock_guard is destructed and the mutex is released.
      std::cout << "local pointer in a thread:\n"
                << "  lp.get() = " << lp.get()
                << ", lp.use_count() = " << lp.use_count() << '\n';
    }
}

int main()
{
    std::shared_ptr<Base> p = std::make_shared<Derived>();
    //Constructs an object of type T and wraps it in a std::shared_ptr
    //using args as the parameter list for the constructor of T.
    //template< class T, class... Args >
    //shared_ptr<T> make_shared( Args&&... args );

    std::cout << "Created a shared Derived (as a pointer to Base)\n"
              << "  p.get() = " << p.get()
              << ", p.use_count() = " << p.use_count() << '\n';
    std::thread t1(thr, p), t2(thr, p), t3(thr, p);
    p.reset(); // release ownership from main
    std::cout << "Shared ownership between 3 threads and released\n"
              << "ownership from main:\n"
              << "  p.get() = " << p.get()
              << ", p.use_count() = " << p.use_count() << '\n';
    t1.join(); t2.join(); t3.join();
    std::cout << "All threads completed, the last one deleted Derived\n";
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值