What is the difference between an empty and a null std::shared_ptr in C++?

It’s a weird corner of shared_ptr behavior. It has a constructor that allows you to make a shared_ptr that owns something and points to something else: template< class Y > shared_ptr( const shared_ptr<Y>& r, T *ptr ); The shared_ptr constructed using this constructor shares ownership with r, but points to whatever ptr points to (i.e., … Read more

C++11 When clearing shared_ptr, should I use reset or set to nullptr?

Is there any real difference, or are there advantages/disadvantages to either approach? The two alternatives are absolutely equivalent, in the sense that the second form (foo = nullptr) is defined in terms of the first one. Per Paragraph 20.7.1.2.3/8-10 of the C++11 Standard: unique_ptr& operator=(nullptr_t) noexcept; 8 Effects: reset(). 9 Postcondition: get() == nullptr 10 … Read more

Where is shared_ptr?

There are at least three places where you may find shared_ptr: If your C++ implementation supports C++11 (or at least the C++11 shared_ptr), then std::shared_ptr will be defined in <memory>. If your C++ implementation supports the C++ TR1 library extensions, then std::tr1::shared_ptr will likely be in <memory> (Microsoft Visual C++) or <tr1/memory> (g++’s libstdc++). Boost … Read more

C++ – passing references to std::shared_ptr or boost::shared_ptr

I found myself disagreeing with the highest-voted answer, so I went looking for expert opinons and here they are. From http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2011-Scott-Andrei-and-Herb-Ask-Us-Anything Herb Sutter: “when you pass shared_ptrs, copies are expensive” Scott Meyers: “There’s nothing special about shared_ptr when it comes to whether you pass it by value, or pass it by reference. Use exactly the … Read more

Should I pass a shared_ptr by reference? [duplicate]

In controlled circumstances you can pass the shared pointer by constant reference. Be sure that nobody is concurrently deleting the object, though this shouldn’t be too hard if you’re careful about to whom you give references. In general, you should pass the shared pointer as a straight copy. This gives it its intended semantics: Every … Read more

Does C++11 unique_ptr and shared_ptr able to convert to each other’s type?

std::unique_ptr is the C++11 way to express exclusive ownership, but one of its most attractive features is that it easily and efficiently converts to a std::shared_ptr. This is a key part of why std::unique_ptr is so well suited as a factory function return type. Factory functions can’t know whether callers will want to use exclusive … Read more

std::shared_ptr of this

There is std::enable_shared_from_this just for this purpose. You inherit from it and you can call .shared_from_this() from inside the class. Also, you are creating circular dependencies here that can lead to resource leaks. That can be resolved with the use of std::weak_ptr. So your code might look like this (assuming children rely on existence of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)