[quote]Hi lemon head. I'm interested in nested classes being able to access "this". In one of your posts you told that the nested object doesn't need to store a reference, that "this can be achieved using the barton-nackmann trick and multi-inheritance, but that's not a nice solution".
Can you give a brief example of the involved technique?[/quote]
I am trying to remember :)
I had already given up on the idea, after seeing that noone is interested in it (including Mr. Bj. Str. in persona - but maybe this is because he gets tons of messages a day)
I hope my C++ is not too rusty in the meantime.
You can use this trick, but then it will no longer be a "nested object", but a "parent class".
I'm not even sure anymore if Barton-Nackman is the correct name for the mechanic. Often it is called "curiously recurring template pattern"
http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern// nested object
class Nested {
virtual void foo_nested() {}
}
class Outside {
void foo_outside() {..}
Nested nested {
void foo_nested() {
// finds the method in class Outside
foo_outside();
}
}; // nested object
}
Outside().nested.foo_nested();
// CRTP solution
template<class T>
class NestedT {
void foo_nested() {
static_cast<T*>(this)->foo_outside();
}
}
class Outside extends NestedT<Outside> {
void foo_outside();
}
Outside().foo_nested();