fork download
  1. template<typename Friend, typename FieldType>
  2. class crazyconst
  3. {
  4. struct identity { typedef Friend type; };
  5. FieldType value;
  6. friend class identity::type;
  7. FieldType& operator=(const FieldType& newValue) { return value = newValue; }
  8. public:
  9. operator FieldType(void) const { return value; }
  10. FieldType operator()(void) const { return value; }
  11. };
  12.  
  13. class A
  14. {
  15. public:
  16. crazyconst<A, int> x;
  17.  
  18. void doStuff()
  19. {
  20. // Gettin' stuff done
  21. x = 5; // OK
  22. }
  23. };
  24.  
  25. int main(int argc, char** argv)
  26. {
  27. A a;
  28. int b = a.x;
  29. int c = a.x(); // also works
  30. }
Success #stdin #stdout 0.01s 2720KB
stdin
Standard input is empty
stdout
Standard output is empty