Created
September 2, 2014 17:14
-
-
Save LB--/51528feec5a44e865abc to your computer and use it in GitHub Desktop.
This file contains 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> | |
#include <memory> | |
struct Base | |
{ | |
virtual ~Base() = default; | |
struct Inner | |
{ | |
Inner(Base &b) | |
: inst(b) | |
{ | |
} | |
virtual ~Inner() = default; | |
virtual void f() = 0; | |
virtual Base &instance() | |
{ | |
return inst; | |
} | |
private: | |
Base &inst; | |
}; | |
std::unique_ptr<Inner> new_inner() | |
{ | |
return std::unique_ptr<Inner>{produce_new_inner()}; | |
} | |
private: | |
virtual Inner *produce_new_inner() = 0; | |
}; | |
struct Derived | |
: virtual Base | |
{ | |
Derived(int x) | |
: v{x} | |
{ | |
} | |
struct Inner | |
: virtual Base::Inner | |
{ | |
Inner(Derived &d) | |
: Base::Inner{d} | |
, inst(d) | |
{ | |
} | |
virtual void f() | |
{ | |
std::cout << inst.v << std::endl; | |
} | |
virtual void g(int x) //just a random example | |
{ | |
inst.v = x; | |
} | |
virtual Derived &instance() //fine | |
{ | |
return inst; | |
} | |
private: | |
Derived &inst; | |
}; | |
std::unique_ptr<Inner> new_inner() | |
{ | |
return std::unique_ptr<Inner>{produce_new_inner()}; | |
} | |
private: | |
virtual Inner *produce_new_inner() override | |
{ | |
return new Inner{*this}; | |
} | |
private: | |
int v; | |
}; | |
int main() | |
{ | |
Derived d {6}; | |
auto inner = d.new_inner(); | |
inner->f(); | |
inner->g(7); | |
inner->f(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment