fork download
  1. #include<iostream>
  2. #include<memory>
  3. #include<vector>
  4.  
  5. using namespace std;
  6.  
  7. struct Test;
  8.  
  9. void fonction (shared_ptr<Test> test) {
  10. cout << "function" << endl;
  11. }
  12.  
  13. struct Test : enable_shared_from_this<Test> {
  14. Test() {
  15. cout << "Construction" << endl;
  16. }
  17.  
  18. ~Test () {
  19. cout << "Destruction" << endl;
  20. }
  21.  
  22. void methode1 () {
  23. cout << "method 1" << endl;
  24. fonction( shared_from_this() );
  25. }
  26.  
  27. void methode2 () {
  28. cout << "method 2" << endl;
  29. }
  30.  
  31. };
  32.  
  33. int main (int argc, char** argv) {
  34. vector<shared_ptr<Test>> tests;
  35. tests.push_back(make_shared<Test>());
  36. Test& t = *tests[0];
  37. t.methode1();
  38. t.methode2();
  39. }
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
Construction
method 1
function
method 2
Destruction