outer class `this` in local classes

Hello all, Is there a way to access the outer class `this` from a local class but without using inheritance? Let me explain with an example: class x { // outer class public: void g() {} void f() { this->g(); // (1) struct y: x { // local class *** using inheritance *** y(x* super): x(*super) {} // (2) void h() { this->g(); // (3) } }; y(this).h(); } }; *** Is there a way to do this but without inheriting `y` from `x`? *** At line (3), the local class `y` can access the outer class `x::g()` simply using `this` because `y` inherits from `x`. However, this inheritance requires to construct another object `x` when `y` is constructed as in line (2) adding runtime overhead and requiring `x` to have an accessible copy constructor. In my application, I need line (3) to essentially look like line (1) so you can program code inside `y` members as if it were inside `x` members. A part from that, the local class `y` could look as it is needed to solve this problem -- with some member variables, other member functions, etc. (For example, I tried to redefine `y::operator->()`/`y::operator*()` to return a pointer/reference to `x` but this does not work because `this->`/`*this` always retain its pointer semantics and they do not use the user defined operators...) Thank you very much. -- Lorenzo

On Thu, Jun 24, 2010 at 1:11 PM, Edward Diener <eldiener@tropicsoft.com> wrote:
On 6/24/2010 1:06 PM, Lorenzo Caminiti wrote:
Hello all,
Is there a way to access the outer class `this` from a local class but without using inheritance? snip...
Shouldn't you be asking this in a C++ NG. It has nothing to do with a Boost library.
Well, I didn't mention it at all but I am dealing with this in trying to implement constant-correct block invariants and loop variants for Boost.Contract with mechanisms inspired by Boost.ScopeExit as indicated by Alexander below. Therefore, the solution of this problem _could_ be related to future Boost libraries... To be honest, I am interested about what Boosters have to say about this problem (because I received many enlightening suggestions before). If you can indicate a couple of good C++ NG I will be happy to pose the same question there. Thanks. On Thu, Apr 15, 2010 at 7:02 PM, Alexander Nasonov <alnsn@yandex.ru> wrote:
vicente.botet wrote:
VBE> Boost.ScopeExit showed a technique that can be used to emulate local functions. Alexander proposed long time ago to provide it independently from ScopeExit but to my knowledge this has not been done.
Sorry for the late reply, I'm not following every boost thread, I got this message via google alerts.
Yes, this has not been done. Local function can borrow argument binding syntax from ScopeExit but there are other things I need to think of. For instance, in
int BOOST_LOCAL_FUNCTION(f, (BOOST_LOCAL_FUNCTION_BIND( (a)(&b) ), int c)) { // ... } BOOST_LOCAL_FUNCTION_END
syntax, is there any way to shorten BOOST_LOCAL_FUNCTION_BIND to "bind"?
The syntax doesn't have to be exactly as above. If someone could take a lead on this it would be great. I don't have time for this, I'm afraid.
-- Lorenzo

On Thu, Jun 24, 2010 at 1:11 PM, Edward Diener <eldiener@tropicsoft.com> wrote:
On 6/24/2010 1:06 PM, Lorenzo Caminiti wrote:
Is there a way to access the outer class `this` from a local class but without using inheritance? snip...
Shouldn't you be asking this in a C++ NG. It has nothing to do with a Boost library.
I have asked this question to Google's group "comp.lang.c++.moderated". Unfortunately, the only answer for my question relies on a C++ `static_cast<>` undefined behavior :(( known to work on most (all?) compilers... Anyways, since I originally posed this question to Boosters, I am referencing the Google's group link here in case anyone is interested to look at the discussion: http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/... -- Lorenzo
participants (2)
-
Edward Diener
-
Lorenzo Caminiti