Created
July 31, 2013 19:03
-
-
Save mastbaum/6125082 to your computer and use it in GitHub Desktop.
A nested C++ class "using private members of its parent"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| class Foo { | |
| public: | |
| class Bar { | |
| public: | |
| Bar(Foo* owner) : owner(owner) {} | |
| int getter() { return owner->a; } | |
| private: | |
| Foo* const owner; | |
| }; | |
| Bar b; | |
| Foo() : b(this), a(42) {} | |
| protected: | |
| int a; | |
| }; | |
| int main(int argc, char* argv[]) { | |
| Foo f; | |
| std::cout << f.b.getter() << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment