简体   繁体   English

如何从 C++ 内部 class 引用封闭实例?

[英]How to refer to enclosing instance from C++ inner class?

In C++, an object refers to itself via this .在 C++ 中,object 通过this引用自身。

But how does an instance of an inner class refer to the instance of its enclosing class?但是内部 class 的实例如何引用其封闭的 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 .我对非静态内部类如何工作的理解是Bear可以访问其Zoo的成员,因此它有一个指向Zoo的隐式指针。 I don't want to access the members in this case;在这种情况下,我不想访问成员; I'm trying to get that implicit pointer.我正在尝试获取那个隐式指针。

Unlike Java, inner classes in C++ do not have an implicit reference to an instance of their enclosing class.与 Java 不同,C++ 中的内部类没有对其封闭 class 实例的隐式引用。

You can simulate this by passing an instance, there are two ways:你可以通过传递一个实例来模拟这个,有两种方法:

pass to the method:传递给方法:

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

pass to the constructor:传递给构造函数:

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

        Zoo & zoo;
    };
}; 

Inner classes are not special, and don't have any link to their outer class built-in.内部类并不特殊,并且与内置的外部 class 没有任何链接。 If you want to access the outer class, then pass a pointer or reference, just as you would with any other class.如果要访问外部 class,则传递指针或引用,就像使用任何其他 class 一样。

An inner class has access to all members of the outer class, but it does not have an implicit reference to a parent class instance.内部 class 可以访问外部 class 的所有成员,但它没有对父 class 实例的隐式引用。

To answer your modifed Q:要回答您修改后的问题:
No you cannot access that implicit pointer.不,您无法访问该隐式指针。 I believe one can do so in Java but not in C++.我相信在 Java 中可以这样做,但在 C++ 中则不行。

You will have to pass the outer class object explicitly through constructor or some other function to acheive this.您必须通过构造函数或其他一些 function 显式传递外部 class object 才能实现这一目标。

Technically as per C++03 standard(sec 11.8.1) , a nested class does NOT have special access to its enclosing class.从技术上讲,根据C++03 标准(第 11.8.1 节) ,嵌套的 class 没有对其封闭 class 的特殊访问权限。

But there is also this standard defect: openstd.org/jtc1/sc22/wg21/docs/cwg_defects.html#45 Not sure if this is closed.但也有这个标准缺陷:openstd.org/jtc1/sc22/wg21/docs/cwg_defects.html#45 不确定这是否已关闭。

You can access the outer class instance from an inner class instance by using offsetof.您可以使用 offsetof 从内部 class 实例访问外部 class 实例。
This has zero overhead compared to the pointer/reference solution.与指针/引用解决方案相比,这具有零开销。
It's a bit dirty but it gets the job done.它有点脏,但它完成了工作。
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;
    e.x = 5;
    e.i.printX();
}

PS附言
offsetof makes some assumptions about the type. offsetof 对类型做了一些假设。 In C++98 the type has to be a POD and in C++11 the type has to be "standard layout".在 C++98 中,类型必须是 POD,在 C++11 中,类型必须是“标准布局”。
Here's a reference: http://www.cplusplus.com/reference/cstddef/offsetof/这是一个参考: http://www.cplusplus.com/reference/cstddef/offsetof/

There is no built-in mechanism to achieve this.没有内置机制来实现这一点。 You need to provide the pointer yourself, via constructor or some kind of SetParent function.您需要自己提供指针,通过构造函数或某种 SetParent function。

The C++ Standard says (section 11.8.1 [class.access.nest]): C++ 标准说(第 11.8.1 节 [class.access.nest]):

The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class;嵌套 class 的成员对封闭 class 的成员没有特殊访问权限,也没有对封闭 class 授予友谊的类或函数的特殊访问权限; the usual access rules (clause 11) shall be obeyed.应遵守通常的访问规则(第 11 条)。 The members of an enclosing class have no special access to members of a nested class ;封闭 class 的成员对嵌套 class 的成员没有特殊访问权限 the usual access rules (clause 11) shall be obeyed.应遵守通常的访问规则(第 11 条)。

(emphasis by me). (我强调)。

This means that there is no special relationship between the nested and enclosing class.这意味着嵌套和封闭 class 之间没有特殊关系。

There is no implicitly created instance of the enclosing class when creating an instance of the nested class.在创建嵌套 class 的实例时,没有隐式创建封闭 class 的实例。 It has to be done manually.它必须手动完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C ++中,如何让(嵌套的)比较函子引用封闭类的数据? - In C++, how to let a (nested) compare functor refer to data of the enclosing class? 如何正确引用C ++中其他类的类对象? - How to correctly refer to an object of a class from a different class in C++? C / C ++ - 嵌套类继承自封闭类 - C/C++ - Nested Class Inheriting from Enclosing Class 如何通过封闭 class 访问嵌套的 class 成员 function,其中所有成员在 Z6CE809EACF90BA125B40FA4BD9 - How to access nested class member function from enclosing class where all the members are public in c++? 如何在C ++中从内部类访问外部类对象 - How to access the outer class object from the inner class in c++ 如何使用带有将参数作为封闭类的方法的c ++对象? - How to have a c++ object with a method that takes argument the enclosing class? 如何在C ++中引用“所有者类”? - How to refer to an “owner class” in C++? C++:构建和销毁 static 内部 class 实例在外部 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 - C++: construction and destruction of static inner class instance in outer class C ++:外部类中内部类的静态实例 - C++: static instance of inner class in outer class 如何在C ++中从内部联合访问类成员 - How to access class member from inner union in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM