fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. using std::shared_ptr;
  5. using std::weak_ptr;
  6. using std::make_shared;
  7.  
  8. class A : public std::enable_shared_from_this<A>
  9. {
  10. public:
  11.  
  12. class B{
  13. public:
  14. B(shared_ptr<A> ptr) { //??
  15. ptrToA = ptr;
  16. }
  17. ~B(){}
  18. private:
  19. int bNumber{1};
  20. weak_ptr<A> ptrToA;
  21. };
  22.  
  23.  
  24. A(){
  25. ptrToB = make_shared<B>(shared_from_this());
  26. }
  27. ~A(){}
  28. shared_ptr<B> getPtrToB() { return ptrToB; }
  29.  
  30.  
  31.  
  32. private:
  33. shared_ptr<B> ptrToB;
  34. int aNumber{0};
  35. };
  36.  
  37. int main(){
  38. std::shared_ptr<A> aInstance;
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 4560KB
stdin
Standard input is empty
stdout
Standard output is empty