How to access a nested instance from a C ++ inner class?

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

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

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

EDIT

My understanding of how non-static inner classes work is that Bear can access members of its Zoo , so it has an implicit pointer to Zoo . I do not want to contact members in this case; I am trying to get this implicit pointer.

+7
source share
7 answers

Unlike Java, inner classes in C ++ do not have an implicit reference to an instance of their enclosing class.

You can simulate this by passing an instance, there are two ways:

go to method:

 class Zoo { class Bear { void runAway( Zoo & zoo) { EscapeService::helpEscapeFrom ( this, /* the Bear */ zoo ); } }; }; 

go to constructor:

 class Zoo { class Bear { Bear( Zoo & zoo_ ) : zoo( zoo_ ) {} void runAway() { EscapeService::helpEscapeFrom ( this, /* the Bear */ zoo ); } Zoo & zoo; }; }; 
+13
source

Inner classes are not special and have no reference to their outer class. If you want to access an external class, pass a pointer or a link in the same way as with any other class.

+6
source

The inner class has access to all members of the outer class, but does not have an implicit reference to an instance of the parent class.

To answer your modified question:
No, you cannot access this implicit pointer. I believe that this can be done in Java, but not in C ++.

You will have to pass the object of the outer class explicitly through the constructor or some other function to achieve this.

Technically, in accordance with the C ++ 03 standard (since 11.8.1) , a nested class does NOT have special access to its encompassing class.

But there is this standard defect: openstd.org/jtc1/sc22/wg21/docs/cwg_defects.html#45 Not sure if it is closed.

+5
source

You can access an instance of an outer class from an inner instance of the class using offsetof.
This has zero overhead compared to a pointer / reference solution.
He is a little dirty, but he is doing his job.
For example:

 #include <cstddef> struct enclosing { struct inner { enclosing& get_enclosing() { return *(enclosing*)((char*)this - offsetof(enclosing, i)); } void printX() { std::cout << get_enclosing().x << '\n'; } } i; int x; }; int main() { enclosing e; ex = 5; eiprintX(); } 

PS
offsetof makes some assumptions about the type. In C ++ 98, the type must be a POD, and in C ++ 11, the type must be a "standard layout."
Here's the link: http://www.cplusplus.com/reference/cstddef/offsetof/

+3
source

There is no built-in mechanism for this. You need to provide the pointer yourself, through the constructor or some SetParent function.

+2
source

In the C ++ standard (section 11.8.1 [class.access.nest]):

Members of a nested class do not have special access to members encompassing the class, as well as classes or functions that gave friendship to the closing class; usually the access rules (clause 11) must be obeyed. Members of the closing class do not have special access to members of the nested class ; usually the access rules (clause 11) must be obeyed.

(highlighted by me).

This means that there is no special relationship between the enclosed and the enclosing class.

+2
source

When creating an instance of a nested class, there is no implicitly created instance of the surrounding class. This must be done manually.

+1
source

All Articles