2

只需浏览我最喜欢的一本书(Ellen Ullman 的 The Bug),就会发现一个程序员在三个间接级别上遇到另一个程序员:

***object_array = ***winarray;

我得到了双重间接的想法——一种将指针传递给函数的方法,并允许它指向在函数中创建的对象。

但是你有没有遇到过使用三个(或更多)间接级别的理由?

4

4 回答 4

4

当然是
4 维数组。
这样一个数组的应用程序也不需要太多。说某种查找表。我有 8 个或更多维度的查找表。

于 2008-12-11T01:47:14.573 回答
3

正如 David Wheele 所说:“计算机科学中的任何问题都可以通过另一层间接来解决。” 您几乎可以肯定地使用了三层间接,如下所示:

int x = 3;

毕竟,芯片是通过两层 L1 和 L2 缓存来间接访问内存的。操作系统通过虚拟内存页间接访问内存。您的 C# 编译器通过虚拟机中的对象间接访问内存。当然,它并没有一长串的星号,但那是因为所有这些间接都被抽象为机器、操作系统或编译器之类的东西。

于 2008-12-11T02:14:13.130 回答
2

您现在可能正在运行 3 个或更多级别。可以是 jQuery on javascript 在 Mac(或 VM)上通过远程访问在 Mac(或 VM)上运行的浏览器中运行的...。

或者,从更接近您问题上下文的另一个角度来看,3 个级别是我们拥有的最常见的工件。

指向 Window 中容器中控件的指针是什么?

于 2008-12-11T02:19:27.660 回答
2

不,我实际上从未见过或使用过它(据我回忆,至少在没有合理的 typedef 以使其不那么令人毛骨悚然的情况下并非如此),但我可以设计一个可能是 [可疑] 有效用途的示例:

struct Foo{
    struct greater{
        bool operator()(Foo const *a, Foo const *b) const{
            return a->place > b->place ||
                   a->place == b->place && a->holder > b->holder;
        }
    };

    int place;
    int holder;
};

template<typename T, typename Comparer>
void Sort(T const *unorderedList, int count, T const ***orderedList, Comparer &cmp);

void UseOrderedList(Foo const **orderedList, int count);

int main(){
    Foo list[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    Foo const **orderedList;

    Sort(list, sizeof list / sizeof *list, &orderedList, Foo::greater());
    UseOrderedList(orderedList, sizeof list / sizeof *list);
    delete[] orderedList;
    return 0;
}

void UseOrderedList(Foo const **orderedList, int count){/*...*/}

template<typename T, typename Comparer>
void Sort(T const *unorderedList, int count, T const ***orderedList, Comparer &cmp){
    /*
     * The result array stores pointers to the items in the original array.
     * This way, the original array is unmodified, and the result array
     * doesn't create duplicate items.  This makes sense if the objects
     * are large and copying them would be slow (another argument against
     * in-place sorting), or if duplicating them violates some design
     * principle.
     */
    *orderedList = new const T*[count];

    for(int i = 0; i < count; i++)
        (*orderedList)[i] = unorderedList + i;

    std::sort(*orderedList, &(*orderedList)[count], cmp);
}

我实际上不会做我在这里做过的事情。这只是您如何最终获得三个指针级别的示例。不过,我无法想象你经常遇到这种情况。

于 2008-12-11T14:25:50.517 回答