fork(1) download
  1. #include <iostream>
  2. struct BigObj
  3. {
  4. BigObj() : m_useCount(0) {}
  5.  
  6. int m_useCount;
  7. int m_stuff[4096];
  8. };
  9.  
  10. void BigObjByValue(BigObj b)
  11. {
  12. b.m_useCount++;
  13. std::cout << "BigObjByValue " << (&b) << ". m_useCount is now " << b.m_useCount << std::endl;
  14. }
  15.  
  16. void BigObjByPtr(BigObj* b)
  17. {
  18. b->m_useCount++;
  19. std::cout << "BigObjByValue " << (b) << ". m_useCount is now " << b->m_useCount << std::endl;
  20. }
  21.  
  22. void BigObjByConstPtr(const BigObj* b)
  23. {
  24. //b->m_useCount++; // compiler won't allow this, try uncommenting.
  25. std::cout << "BigObjByValue " << (b) << ". m_useCount is now " << b->m_useCount << std::endl;
  26. }
  27.  
  28. int main()
  29. {
  30. BigObj mainB;
  31. std::cout << "Created mainB at " << (&mainB) << " useCount = " << mainB.m_useCount << std::endl;
  32.  
  33. BigObjByValue(mainB);
  34. std::cout << "in main, m_useCount = " << mainB.m_useCount << std::endl;
  35.  
  36. BigObjByPtr(&mainB);
  37. std::cout << "in main, m_useCount = " << mainB.m_useCount << std::endl;
  38.  
  39. BigObjByConstPtr(&mainB);
  40. std::cout << "in main, m_useCount = " << mainB.m_useCount << std::endl;
  41.  
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Created mainB at 0xbfe1eb0c useCount = 0
BigObjByValue 0xbfe1aaf0. m_useCount is now 1
in main, m_useCount = 0
BigObjByValue 0xbfe1eb0c. m_useCount is now 1
in main, m_useCount = 1
BigObjByValue 0xbfe1eb0c. m_useCount is now 1
in main, m_useCount = 1