
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Memory::enable_shared_from_this
For a shared_ptr managed object to acquire another shared_ptr of itself, we need a way to get hold of its control block. This is accomplished by using std::enable_shared_from_this function.
In essence, a handle with access to the control block, which may be either a shared_ptr or a weak_ptr, is the main source from which new shared_ptr instances can be created. An object can generate more shared_ptr for itself if it has that handle. A shared_ptr, on the other hand, acts as a powerful reference and affects the lifespan of the managed object.
Syntax
Following is the syntax for C++ Memory::enable_shared_from_this −
class enable_shared_from_this;
Parameters
T − It's a pointer class.
Example 1
Let's look into the following example, where we are using the enable_shared_from_this and the two shared_ptr's share the same object.
#include <memory> #include <iostream> class Good : public std::enable_shared_from_this<Good>{ public: std::shared_ptr<Good> getptr(){ return shared_from_this(); } }; void check(){ std::shared_ptr<Good> good0 = std::make_shared<Good>(); std::shared_ptr<Good> good = good0->getptr(); std::cout << "good.use_count() = " << good.use_count() << '\n'; } int main(){ check(); }
Output
Let us compile and run the above program, this will produce the following result −
good.use_count() = 2
Example 2
Consider the another scenario, where we are going to use enable_shared_from_this and each shared_ptr thinks they are the only owners of the objecct.
#include <memory> #include <iostream> class Good : public std::enable_shared_from_this<Good>{ public: std::shared_ptr<Good> getptr(){ return shared_from_this(); } }; struct notgood { std::shared_ptr<notgood> getptr(){ return std::shared_ptr<notgood>(this); } ~notgood(){ std::cout << "Welcome\n"; } }; void Check(){ std::shared_ptr<notgood> bad0 = std::make_shared<notgood>(); std::shared_ptr<notgood> bad = bad0->getptr(); std::cout << "bad0.use_count() = " << bad.use_count() << '\n'; } int main(){ Check(); }
Output
On running the above code, it will display the output as shown below −
bad0.use_count() = 1 Welcome double free or corruption (out) Aborted (core dumped)
Example 3
In the following example we are going to make shared_from_this is called without having std::shared_ptr owing the caller.
#include <memory> #include <iostream> class Good : public std::enable_shared_from_this<Good>{ public: std::shared_ptr<Good> getptr(){ return shared_from_this(); } }; void check(){ try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { std::cout << e.what() << '\n'; } } int main(){ check(); }
Output
when the code gets executed, it will generate the output as shown below −
bad_weak_ptr