fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5.  
  6. typedef unsigned long long uint_type;
  7. class sc_uint
  8. {
  9. private: uint_type data = 0xABCD;
  10. public:
  11.  
  12. // User-defined type conversion
  13. operator uint_type () const
  14. {
  15. return data;
  16. }
  17.  
  18. // Method `print (std::ostream &)` inherited from `sc_dt::sc_uint_base
  19. void print (std::ostream & out)
  20. {
  21. out << "Look! I'm a string: "
  22. << std::setw (8) << std::setfill ('0')
  23. << data;
  24. }
  25. };
  26.  
  27.  
  28. // There is no `operator << (ostream &)` in `sc_dt::sc_uint <W>`
  29. // but it could be defined elsewhere
  30. std::ostream & operator << (std::ostream & out, sc_uint & foo)
  31. {
  32. foo.print (out);
  33. return out;
  34. }
  35.  
  36.  
  37. int main ()
  38. {
  39. sc_uint foo;
  40.  
  41. // The `std::ostream & operator << (std::ostream &, sc_uint &)` is
  42. // being called. That is why by default it prints four zeros.
  43. std::cout << std::hex << foo << std::endl;
  44. std::cout << std::dec << foo << std::endl;
  45.  
  46. // The `operator uint_type () const` is now being called turning
  47. // `sc_uint` into `uint_type`, which is `long long`. The `std::cout`
  48. // skips leading zeros.
  49. std::cout << std::hex << (foo & 0xFF) << std::endl;
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Look! I'm a string: 0000abcd
Look! I'm a string: 00043981
cd