I was wondering something like this is possible. Perhaps using templates and trickery?
The only rule right now i’ll enforce is you are NOT ALLOWED to use a constructor in the class bar (so you cannot pass in a reference or pointer to foo nor a).
class Foo{
int a;
public:
struct Bar{
int barme(){ return a; }
};
};
I know one problem is how does bar know what instance of foo to use? i dont know but i’d like a foo to have many classes of Bar and more then one foo which each will have more then one bar.
I’m sure i seen a trick somewhere (maybe in a different language) but i dont know where. I do remember a template using compile time const but foo wont be compile time in this case although its ptr/ref could be
In C++ nested classes are just classes. It’s not like Java where a nested class comes with an instance of the parent class along for the ride. Therefore, nothing like this is possible. You have to pass an instance of
Footo operate on insideBar. (For examplestd::vector::iteratordoes not come with astd::vector— it’s a completely independent type)