{"id":1753,"date":"2021-02-10T16:46:22","date_gmt":"2021-02-10T21:46:22","guid":{"rendered":"http:\/\/www.jaimerios.com\/?p=1753"},"modified":"2025-12-02T23:38:02","modified_gmt":"2025-12-02T23:38:02","slug":"using-shared_ptr-a-short-article","status":"publish","type":"post","link":"https:\/\/jaimerios.com\/?p=1753","title":{"rendered":"Using shared_ptr, a short article"},"content":{"rendered":"\n<p>How can I create a shared_ptr?<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ Example 1 - you can see that you construct a new shared_ptr with a pointer\n\/\/ to my_class\nstd::shared_ptr&lt;my_class> example1 = std::shared_ptr&lt;my_class>(new my_class());\n\n\/\/ Example 2 - you can also use the convenience function make_shared to make\n\/\/ it easier to type\nstd::shared_ptr&lt;my_class> example2 = std::make_shared&lt;my_class>();\n\n\/\/ Example 3 - and if you really want to go full blown modern, use auto so\n\/\/ that you don't have to type the type for the variable declaration\nauto example3 = std::make_shared&lt;my_class>();<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ Example 1 - you can see that you construct a new shared_ptr with a pointer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ to my_class<\/span><\/span>\n<span class=\"line\"><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::shared_ptr&lt;my_class&gt; example1 = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">shared_ptr<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt;(<\/span><span style=\"color: #C586C0\">new<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">my_class<\/span><span style=\"color: #D4D4D4\">());<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Example 2 - you can also use the convenience function make_shared to make<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ it easier to type<\/span><\/span>\n<span class=\"line\"><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::shared_ptr&lt;my_class&gt; example2 = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">make_shared<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt;();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Example 3 - and if you really want to go full blown modern, use auto so<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ that you don&#39;t have to type the type for the variable declaration<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\"> example3 = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">make_shared<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt;();<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>How about a NULL shared_ptr?<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>std::shared_ptr&lt;my_class> my_nullptr;\nor\nauto my_nullptr = std::shared_ptr&lt;my_class>();<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::shared_ptr&lt;my_class&gt; my_nullptr;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">or<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\"> my_nullptr = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">shared_ptr<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt;();<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>What if I already have a shared_ptr, that I own, and I want to null it out?<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>auto my_ptr = std::make_shared&lt;my_class>();\n\n\/\/ Let's pretend to do some stuff\nmy_ptr.reset();<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\"> my_ptr = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">make_shared<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt;();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Let&#39;s pretend to do some stuff<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">my_ptr<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">reset<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Ok, ok. What if I want to null out my shared_ptr and at the same time, replace it with a new instance?<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>auto my_ptr = std::make_shared&lt;my_class>();\n\n\/\/ Let's pretend to do some stuff; let's reset it with a new instance\nmy_ptr.reset(new my_class());<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\"> my_ptr = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">make_shared<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt;();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ Let&#39;s pretend to do some stuff; let&#39;s reset it with a new instance<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">my_ptr<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">reset<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #C586C0\">new<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">my_class<\/span><span style=\"color: #D4D4D4\">());<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>How do I check if the shared_ptr is still in use?<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>auto my_ptr = std::make_shared&lt;my_class>();\n\n\/\/ More pretending ...\nprintf(\"Reference count: %ld\\n\", my_ptr.use_count();<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">auto<\/span><span style=\"color: #D4D4D4\"> my_ptr = <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #DCDCAA\">make_shared<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt;();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ More pretending ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #DCDCAA\">printf<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Reference count: <\/span><span style=\"color: #9CDCFE\">%ld<\/span><span style=\"color: #D7BA7D\">\\n<\/span><span style=\"color: #CE9178\">&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">my_ptr<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">use_count<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>What if I&#8217;m in a class function and want to take my current instance and pass it to another function as a shared_ptr?<br>A:  So long as your class is already managed by an existing shared_ptr, you can:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 0 16px;font-size:0.8em;width:100%;text-align:left;background-color:#1E1E1E;font-style:italic;color:#D4D4D4\"><span style=\"border-bottom:1px solid rgba(234, 191, 191, 0.2)\">C++<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>\/\/ Add this at the class declaration:\nclass my_class : public std::enable_shared_from_this&lt;my_class>\n\n\/\/ And add this member function\nstd::shared_ptr&lt;my_class> get_shared_ptr()\n{\nreturn shared_from_this();\n}<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\">\/\/ Add this at the class declaration:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\"> : <\/span><span style=\"color: #569CD6\">public<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">std<\/span><span style=\"color: #D4D4D4\">::<\/span><span style=\"color: #4EC9B0\">enable_shared_from_this<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/\/ And add this member function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">std::<\/span><span style=\"color: #4EC9B0\">shared_ptr<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #4EC9B0\">my_class<\/span><span style=\"color: #D4D4D4\">&gt; get_shared_ptr()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">shared_from_this<\/span><span style=\"color: #D4D4D4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>That&#8217;s pretty much it. Just remember that you need the public keyword before std::enable_shared_from_this, otherwise you would get some weird behavior when you call get_shared_ptr.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How can I create a shared_ptr? How about a NULL shared_ptr? What if I already have a shared_ptr, that I own, and I want to null it out? Ok, ok. What if I want to null out my shared_ptr and at the same time, replace it with a new instance? How do I check if &#8230; <a title=\"Using shared_ptr, a short article\" class=\"read-more\" href=\"https:\/\/jaimerios.com\/?p=1753\" aria-label=\"Read more about Using shared_ptr, a short article\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[143],"class_list":["post-1753","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cpp"],"_links":{"self":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1753"}],"version-history":[{"count":1,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1753\/revisions"}],"predecessor-version":[{"id":1895,"href":"https:\/\/jaimerios.com\/index.php?rest_route=\/wp\/v2\/posts\/1753\/revisions\/1895"}],"wp:attachment":[{"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaimerios.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}