Skip to content

Instantly share code, notes, and snippets.

@mastbaum
Created July 31, 2013 19:03
Show Gist options
  • Select an option

  • Save mastbaum/6125082 to your computer and use it in GitHub Desktop.

Select an option

Save mastbaum/6125082 to your computer and use it in GitHub Desktop.
A nested C++ class "using private members of its parent"
#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