How to refer to enclosing instance from C++ inner

2020-02-01 04:14发布

In C++, an object refers to itself via this.

But how does an instance of an inner class refer to the instance of its enclosing class?

class Zoo
{
    class Bear 
    {
        void runAway()
        {
            EscapeService::helpEscapeFrom (
                this, /* the Bear */ 
                ??? /* I need a pointer to the Bear's Zoo here */);
        }
    };
};

EDIT

My understanding of how non-static inner classes work is that Bear can access the members of its Zoo, therefore it has an implicit pointer to Zoo. I don't want to access the members in this case; I'm trying to get that implicit pointer.

7条回答
够拽才男人
2楼-- · 2020-02-01 04:59

There is no built-in mechanism to achieve this. You need to provide the pointer yourself, via constructor or some kind of SetParent function.

查看更多
登录 后发表回答