2018-cpp学习汇总

1.boost的scoped_ptr在reset时会交换,如果和空指针交换,原数据在什么时候析构

 92     void reset(T * p = 0) // never throws
 93     {
 94         BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
 95         this_type(p).swap(*this);
 96         int a= 10;
 97         a++;
 98         printf("%d\n",a);
 99     }
121     void swap(scoped_ptr & b) BOOST_NOEXCEPT
122     {
123         printf("swap begin\n");
124         T * tmp = b.px;
125         b.px = px;
126         px = tmp;
127         printf("swap end\n");
128     }

=========================
  7 struct p_file
  8 {
  9     p_file(const char * file_name)
 10     {
 11         cout<< "open file" << file_name << endl;
 12     }
 13     ~p_file()
 14     {
 15         cout << "close file" <<endl;
 16     }
 17 };

 38     boost::scoped_ptr<p_file> fp(new p_file("/tmp/a.txt"));
 39     fp.reset();
 40     cout << "111" <<endl;

open file/tmp/a.txt
swap begin
swap end
close file
11
111

会在swap结束后reset结束前析构,原因不明

  

posted @ 2018-07-19 14:10  dodng  阅读(264)  评论(0)    收藏  举报