Guest User

Untitled

a guest
Jul 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. struct Inner {
  2. int field = 0;
  3. void Modify() {
  4. field++;
  5. }
  6. };
  7.  
  8. struct Outer {
  9. Inner inner;
  10. };
  11.  
  12. class MyClass {
  13. public:
  14. Outer outer;
  15. Inner& inner; // refers to outer.inner, for convenience
  16.  
  17. MyClass() : inner(outer.inner) {}
  18.  
  19. void ConstMethod() const {
  20. inner.Modify(); // oops; compiles
  21. }
  22. };
  23.  
  24. int main() {
  25. const MyClass myclass;
  26. std::cout << myclass.outer.inner.field << "n"; // prints 0
  27. myclass.ConstMethod();
  28. std::cout << myclass.outer.inner.field << "n"; // prints 1
  29. }
Add Comment
Please, Sign In to add comment