Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Inner {
- int field = 0;
- void Modify() {
- field++;
- }
- };
- struct Outer {
- Inner inner;
- };
- class MyClass {
- public:
- Outer outer;
- Inner& inner; // refers to outer.inner, for convenience
- MyClass() : inner(outer.inner) {}
- void ConstMethod() const {
- inner.Modify(); // oops; compiles
- }
- };
- int main() {
- const MyClass myclass;
- std::cout << myclass.outer.inner.field << "n"; // prints 0
- myclass.ConstMethod();
- std::cout << myclass.outer.inner.field << "n"; // prints 1
- }
Add Comment
Please, Sign In to add comment