why the shared_ptr can convert to this pointer?

why the shared_ptr can convert to this pointer?

I'm studying enable_shared_from_this of C++11 now. one example made me confused, how the shared_ptr type returned by shared_from_this() can convert to this saw pointer?

I'm studying enable_shared_from_this of C++11 now. one example made me confused, how the shared_ptr type returned by shared_from_this() can convert to this saw pointer?

#include <iostream>
#include <memory>
#include <functional>

struct Bar { Bar(int a) : a(a) {} int a; };

struct Foo : public std::enable_shared_from_this<Foo> { Foo() { std::cout << "Foo::Foo\n"; } Foo() { std::cout << "Foo::Foo\n"; }

std::shared_ptr&lt;Bar&gt; getBar(int a)
{
    std::shared_ptr&lt;Bar&gt; pb(new Bar{a},
                            std::bind(&amp;Foo::showInfo,
                                      shared_from_this(),
                                      std::placeholders::_1));
    return pb;
}

void showInfo(Bar *pb)
{
    std::cout &lt;&lt; "Foo::showInfo()\n";
    delete pb;
}

};

int main() { std::shared_ptr<Foo> pf(new Foo); std::shared_ptr<Bar> pb = pf->getBar(10); std::cout << "pf use_count: " << pf.use_count() << std::endl; }


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Variable Introduction in C#[Bangla]

LIKE | COMMENT | SHARE | SUBSCRIBE A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...