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";
}
阅读更多
版权声明:http://blog.csdn.net/c337134154 https://blog.csdn.net/c337134154/article/details/49964619
个人分类: effective c++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭