fork download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <memory>
  5. #include <string>
  6. #include <type_traits>
  7. #include <utility>
  8.  
  9.  
  10. class A : public std::enable_shared_from_this<A>
  11. {};
  12.  
  13. class B : public A
  14. {
  15. public:
  16. void doReallyCoolStuff();
  17. };
  18.  
  19. void doCoolStuff(std::weak_ptr<A> obj)
  20. {
  21. std::cout << "cool A\n";
  22. }
  23.  
  24. void doCoolStuff(std::weak_ptr<B> obj)
  25. {
  26. std::cout << "cool B\n";
  27. doCoolStuff(std::static_pointer_cast<A>(obj.lock()));
  28. }
  29.  
  30. void B::doReallyCoolStuff()
  31. {
  32. doCoolStuff(std::weak_ptr<B>(std::static_pointer_cast<B>(shared_from_this())));
  33. }
  34.  
  35. int main()
  36. {
  37. auto b = std::make_shared<B>();
  38. b->doReallyCoolStuff();
  39. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
cool B
cool A