Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中从内部类对象获取外部类对象+; 问题_C++_Oop - Fatal编程技术网

C++ 在C+中从内部类对象获取外部类对象+; 问题

C++ 在C+中从内部类对象获取外部类对象+; 问题,c++,oop,C++,Oop,我在Java上看到了一个问题,它允许您从嵌套对象中获取指向外部对象的指针 但是如何在C++中实现这一点呢 不满意的解决方案: 存储指向每个对象的指针(不节省内存) 将数组包装到类中(增加了不必要的(?)复杂性) 这里有一个节省空间的想法: struct Outer { int i; struct Inner { int j; uint16_t where; Outer& outer() { Inn

我在Java上看到了一个问题,它允许您从嵌套对象中获取指向外部对象的指针

但是如何在C++中实现这一点呢

不满意的解决方案: 存储指向每个对象的指针(不节省内存)

将数组包装到类中(增加了不必要的(?)复杂性)


这里有一个节省空间的想法:

struct Outer {
    int i;

    struct Inner {
        int j;
        uint16_t where;

        Outer& outer() {
            Inner* first = this - where;
            char* addr = reinterpret_cast<char*>(first) - offsetof(Outer, items);
            return *reinterpret_cast<Outer*>(addr);
        }
    };

    Inner items[1000];

    Outer() {
        for (uint16_t ii = 0; ii < 1000; ++ii)
            items[ii].where = ii;
    }
};

但是
outer()
是O(n)而不是O(1),并且您必须确保
INT\u MIN
(或某些哨兵值)从未在
项中使用

,不幸的是,对于非POD类来说,他的行为是未定义的:stackoverflow.com/questions/1129894/。不过我尊重这个想法:)@GillBates:如果您认为您的平台没有以预期的方式实现
offsetof()
,请硬编码。如果你有一个健全的编译器,你知道偏移量实际上是什么。但是,一个理智的编译器将很容易使
offsetof()
工作……@GillBates:我添加了第二个完全不同的解决方案——请参阅。它仍然使用
offsetof
,但这一部分并不重要——如果需要避免
offsetof()
,您可以将
sentinel
移动到
外部
的开头。这些都不是傻瓜式的解决方案,但我知道这是我们能做的最好的。我想在公司代码中使用它,所以我想我只需要额外的指针,或者可能是一个带有静态指针的单一外部类。感谢您的贡献。在Java中,内部类的每个对象都有一个指向其父外部类对象的隐式隐藏指针。如此多的内存效率低下的C++。@ N.M:(这让我很伤心…为什么只有一堆指针,只要1需要?
class Outer {
private:
    int i;
    class Inner_array {
        class Inner {
            int j;
        };
        Inner items[1000];

        // Build an interface around the array
        typedef Inner (&array)[1000]; 
        operator array();
        // etc...
    };
            
    Inner items[1000];
    Outer *outer;
};
    
struct Outer {
    int i;

    struct Inner {
        int j;
        uint16_t where;

        Outer& outer() {
            Inner* first = this - where;
            char* addr = reinterpret_cast<char*>(first) - offsetof(Outer, items);
            return *reinterpret_cast<Outer*>(addr);
        }
    };

    Inner items[1000];

    Outer() {
        for (uint16_t ii = 0; ii < 1000; ++ii)
            items[ii].where = ii;
    }
};
struct Outer {
    int i;

    struct Inner {
        int j;

        Outer& outer() {
            Inner* sentinel = this;
            while (sentinel.j != INT_MIN)
                --sentinel;
            char* addr = reinterpret_cast<char*>(sentinel) - offsetof(Outer, sentinel);
            return *reinterpret_cast<Outer*>(addr);
        }
    };

    Inner sentinel = {INT_MIN};
    Inner items[1000];
};