TEST(SmartPointerTests, SharedPtr) { { std::shared_ptr<Foo> sh1; EXPECT_EQ(sh1.use_count(), 0); } { Foo* foo = new Foo(); std::shared_ptr<Foo> sh2(foo); std::shared_ptr<Foo> sh3(sh2); std::shared_ptr<Foo> sh4; std::shared_ptr<Foo> sh5; sh4 = sh2; sh5 = std::shared_ptr<Foo>(sh2); EXPECT_EQ(sh2.use_count(), 4); EXPECT_EQ(sh3.use_count(), 4); EXPECT_EQ(sh4.use_count(), 4); EXPECT_EQ(sh5.use_count(), 4); } }
int main() { { Foo *f =new Foo; std::shared_ptr<Foo> sh2(f); std::shared_ptr<Foo> sh3(f); std::cout << sh2.use_count() << '\n'; std::cout << sh3.use_count() << '\n'; } }
int main(void) { double r1, r2, r3, x; int i; x = 10.0*10.0*10.0; printf("10^i\tsh1\t\tsh2\t\tsh3\t\terr(sh2)\terr(sh3)\n"); for(i = 2; i > -N; i--) { x /= 10.0; r1 = sh1(x); r2 = sh2(x); r3 = sh3(x); printf("10^%d\t%e\t%e\t%e\t%e\t%e\n", i, r1, r2, r3, (r1-r2)/r1, (r1-r3)/r1); } return 0; }
int main() { { std::cout << "constructor with no managed object\n"; jle::shared_ptr<Foo> sh1; } { std::cout << "constructor with object\n"; jle::shared_ptr<Foo> sh2(new Foo); jle::shared_ptr<Foo> sh3(sh2); std::cout << sh2.use_count() << '\n'; std::cout << sh3.use_count() << '\n'; } { std::cout << "constructor with object and deleter\n"; jle::shared_ptr<Foo> sh4(new Foo, D()); } { std::cout << "valid pointer" << std::endl; auto ptr = jle::make_shared<int>(22); std::cout << "expired " << int(ptr.expired()) << std::endl; } { std::cout << "invalid pointer" << std::endl; auto ptr = jle::make_shared<int>(22); ptr.reset(); std::cout << "expired " << int(ptr.expired()) << std::endl; } { std::cout << "invalid pointer" << std::endl; jle::shared_ptr<int> ptr; std::cout << "expired " << int(ptr.expired()) << std::endl; } { jle::shared_ptr<int> foo = jle::make_shared<int> (10); // same as: jle::shared_ptr<int> foo2 (new int(10)); auto bar = jle::make_shared<int> (20); auto baz = jle::make_shared<std::pair<int,int>> (30,40); std::cout << "*foo: " << *foo << '\n'; std::cout << "*bar: " << *bar << '\n'; std::cout << "*baz: " << baz->first << ' ' << baz->second << '\n'; } { std::cout << "invalid pointer" << std::endl; auto ptr = jle::make_shared<int>(22); ptr = {}; std::cout << "expired " << int(ptr.expired()) << std::endl; } }