strtref.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // strtref.cpp -- using structure references
  2. #include <iostream>
  3. using namespace std;
  4. struct sysop
  5. {
  6. char name[26];
  7. char quote[64];
  8. int used;
  9. };
  10. const sysop & use(sysop & sysopref); // function with a reference return type
  11. int main()
  12. {
  13. // NOTE: some implementations require using the keyword static
  14. // in the two structure declarations to enable initialization
  15. sysop looper =
  16. {
  17. "Rick \"Fortran\" Looper",
  18. "I'm a goto kind of guy.",
  19. 0
  20. };
  21. use(looper); // looper is type sysop
  22. cout << "Looper: " << looper.used << " use(s)\n";
  23. sysop copycat;
  24. copycat = use(looper);
  25. cout << "Looper: " << looper.used << " use(s)\n";
  26. cout << "Copycat: " << copycat.used << " use(s)\n";
  27. cout << "use(looper): " << use(looper).used << " use(s)\n";
  28. // cin.get();
  29. return 0;
  30. }
  31. // use() returns the reference passed to it
  32. const sysop & use(sysop & sysopref)
  33. {
  34. cout << sysopref.name << " says:\n";
  35. cout << sysopref.quote << endl;
  36. sysopref.used++;
  37. return sysopref;
  38. }