双指针有什么实际用途? [英] What real use does a double pointer have?

查看:94
本文介绍了双指针有什么实际用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索并找到了答案,但是找不到我真正得到"的东西.

I have searched and searched for an answer to this but can't find anything I actually "get".

我对C ++非常陌生,无法理解使用双,三指针等.它们的意义是什么?

I am very very new to c++ and can't get my head around the use of double, triple pointers etc.. What is the point of them?

任何人都可以启发我

推荐答案

老实说,在编写良好的C ++中,您很少会在库代码之外看到T**.实际上,拥有的星星越多,您就越有可能赢得具有某种性质的奖励.

Honestly, in well-written C++ you should very rarely see a T** outside of library code. In fact, the more stars you have, the closer you are to winning an award of a certain nature.

这并不是说永远都不需要指针到指针.出于与构造任何其他类型对象的指针相同的原因,您可能需要构造一个指向该指针的指针.

That's not to say that a pointer-to-pointer is never called for; you may need to construct a pointer to a pointer for the same reason that you ever need to construct a pointer to any other type of object.

特别是,当您在动态分配的节点周围拖曳时,我可能希望在数据结构或算法实现中看到这种情况?

In particular, I might expect to see such a thing inside a data structure or algorithm implementation, when you're shuffling around dynamically allocated nodes, perhaps?

通常,尽管在这种情况下,如果您需要传递对指针的引用,则只需这样做(即T*&),而不是加倍使用指针,甚至应该是相当罕见的.

Generally, though, outside of this context, if you need to pass around a reference to a pointer, you'd do just that (i.e. T*&) rather than doubling up on pointers, and even that ought to be fairly rare.

在堆栈溢出中,您将看到人们在做可怕的事情时,使用指向动态分配的数据指针数组的指针,试图实现他们认为的效率最低的"2D向量".请不要被他们启发.

On Stack Overflow you're going to see people doing ghastly things with pointers to arrays of dynamically allocated pointers to data, trying to implement the least efficient "2D vector" they can think of. Please don't be inspired by them.

总而言之,您的直觉并非一无是处.

In summary, your intuition is not without merit.

这篇关于双指针有什么实际用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆