reset_pointer.pass.cpp revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
1//===----------------------------------------------------------------------===// 2// 3// ��������������������The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10// <memory> 11 12// shared_ptr 13 14// template<class Y> void reset(Y* p); 15 16#include <memory> 17#include <cassert> 18 19struct B 20{ 21 static int count; 22 23 B() {++count;} 24 B(const B&) {++count;} 25 virtual ~B() {--count;} 26}; 27 28int B::count = 0; 29 30struct A 31 : public B 32{ 33 static int count; 34 35 A() {++count;} 36 A(const A&) {++count;} 37 ~A() {--count;} 38}; 39 40int A::count = 0; 41 42int main() 43{ 44 { 45 std::shared_ptr<B> p(new B); 46 A* ptr = new A; 47 p.reset(ptr); 48 assert(A::count == 1); 49 assert(B::count == 1); 50 assert(p.use_count() == 1); 51 assert(p.get() == ptr); 52 } 53 assert(A::count == 0); 54 { 55 std::shared_ptr<B> p; 56 A* ptr = new A; 57 p.reset(ptr); 58 assert(A::count == 1); 59 assert(B::count == 1); 60 assert(p.use_count() == 1); 61 assert(p.get() == ptr); 62 } 63 assert(A::count == 0); 64} 65