GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — modern-cpp-tutorial/code/5/5.1.shared.ptr.a.cpp at master · xduooo/modern-cpp-tutorial Expand file tree/ Copy path5.1.shared.ptr.a.cppMore file actions 52 lines (42 loc) · 1.75 KB / Copy path5.1.shared.ptr.a.cppFile metadata and controls52 lines (42 loc) · 1.75 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 // // 5.1.shared.ptr.cpp // chapter 05 start pointers and memory management // modern c++ tutorial // // created by changkun at changkun.de // https://github.com/changkun/modern-cpp-tutorial // #include <iostream> #include <memory> void foo(std::shared_ptr<int> i) { (*i)++; } int main() { // auto pointer = new int(10); // illegal, no direct assignment // std::shared_ptr construction auto pointer = std::make_shared<int>(10); auto pointer2 = pointer; // reference count + 1 auto pointer3 = pointer; // reference count + 1 foo(pointer); std::cout << *pointer << std::endl; // 11 int *p = pointer.get(); // does not increase reference count std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; pointer2.reset(); std::cout << "reset pointer2:" << std::endl; std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; pointer3.reset(); std::cout << "reset pointer3:" << std::endl; std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // std::cout << *pointer << std::endl; // reference count equals 0, illegal access // Before leaving the scope, the pointer is destructed and // the reference count is reduced to 0 return 0; } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |