fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Person
  5. {
  6. public:
  7. Person(const string& pN = "no name");
  8. Person(const Person &p);
  9. ~Person();
  10. private:
  11. string pName;
  12. };
  13.  
  14. Person::Person(const string& pN) : pName(pN)
  15. {
  16. cout << "Constructing Person " << pN << '\n';
  17. }
  18.  
  19. Person::Person(const Person &p) : pName(p.pName)
  20. {
  21. cout << "Copying " << p.pName << " into its own block\n";
  22. }
  23.  
  24. Person::~Person()
  25. {
  26. cout << "Destructing " << pName << '\n';
  27. }
  28.  
  29. Person fun(Person a)
  30. {
  31. Person ms(a);
  32. return ms;
  33. }
  34.  
  35. int main()
  36. {
  37. Person p("Amadis");
  38. Person s = fun(p);
  39. Person ms("Kansas");
  40. Person ns = ms;
  41. }
  42.  
Success #stdin #stdout 0.02s 2816KB
stdin
Standard input is empty
stdout
Constructing Person Amadis
Copying Amadis into its own block
Copying Amadis into its own block
Destructing Amadis
Constructing Person Kansas
Copying Kansas into its own block
Destructing Kansas
Destructing Kansas
Destructing Amadis
Destructing Amadis