Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. // Color.h
  2. class Color {
  3. protected:
  4. int id;
  5. Color(int id) : id(id) {}
  6. void operator&(); //undefined
  7. public:
  8. Color(const Color& r) : id(r.id) {}
  9. Color& operator=(const Color& r) {id=r.id; return *this;}
  10. bool operator==(const Color& r) const {return id==r.id;}
  11. bool operator!=(const Color& r) const {return id!=r.id;}
  12. operator int() const {return id;} //so you can still switch on it
  13.  
  14. static Color Blue;
  15. static Color Red;
  16. };
  17.  
  18. // Color.cpp
  19. #include "Color.h"
  20.  
  21. Color Color::Blue(0);
  22. Color Color::Red(1);
  23.  
  24. // main.cpp
  25. #include "Test.h"
  26. using namespace std;
  27.  
  28. const Color mainColors[] = {Color::Red, Color::Red }; // values should be [1, 1]
  29.  
  30. int main()
  31. {
  32. cout << "Main file-scoped colors: " << mainColors[0] << ", " << mainColors[1] << endl; // prints [1, 1]
  33. Test();
  34. return 0;
  35. }
  36.  
  37. // Test.h
  38. #include "Color.h"
  39. void Test();
  40.  
  41. // Test.cpp
  42. #include "Test.h"
  43. const Color fooColors[] = {Color::Red, Color::Red}; // values should be [1, 1]
  44.  
  45. void Test()
  46. {
  47. cout << "Test file-scoped colors: " << fooColors[0] << ", " << fooColors[1] << endl; // prints [0, 0]
  48. }
  49.  
  50. static Color Blue();
  51. static Color Red();
  52.  
  53. Color Color::Blue() {return Color(0);}
  54. Color Color::Red() {return Color(1);}
  55.  
  56. const Color mainColors[] = {Color::Blue(), Color::Red() };
  57.  
  58. const Color fooColors[] = {Color::Blue(), Color::Red()};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement