fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. namespace test {
  5. struct myClass {
  6. std::string _name;
  7.  
  8. explicit myClass(std::string name) : _name{ name } {}
  9. void print(std::ostream& os) const { os << _name; }
  10. };
  11.  
  12. namespace release {
  13. std::ostream& operator<<(std::ostream& os, const myClass& mC) {
  14. mC.print(os);
  15. return os;
  16. }
  17. }
  18.  
  19. namespace debug {
  20. std::ostream& operator<<(std::ostream& os, const myClass& mC) {
  21. os << "debug: ";
  22. mC.print(os);
  23. return os;
  24. }
  25. }
  26. }
  27.  
  28. test::myClass mc{"Kai"};
  29.  
  30. void run() {
  31. using namespace test::release;
  32. std::cout << mc << std::endl;
  33. }
  34.  
  35. void runDebug() {
  36. using namespace test::debug;
  37. std::cout << mc << std::endl; // das erzeugt nun keinen Fehler mehr
  38. //test::debug::operator<<(std::cout, mc) << std::endl; // so klappt das
  39.  
  40. }
  41.  
  42. int main() {
  43. run();
  44. runDebug();
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 5524KB
stdin
Standard input is empty
stdout
Kai
debug: Kai