doujian3401 2012-01-07 08:23
浏览 19
已采纳

指向指针有什么好处?

In the Go programming language; how can pointers to pointers become useful?

(Why are they not illegal if they are not really useful?)

  • 写回答

5条回答 默认 最新

  • douxian9943 2012-01-07 10:14
    关注

    The usefulness of any data type depends on the problem being solved and on the method used to solve the problem. If a data type does not fit the problem, it simply does not fit the problem - and there is nothing more to it.

    The Go programming language (as well as most other programming languages) is based on simple rules that the programmer can use to build new data types. Some of these rules are:

    • *T: create a new data type that is a pointer to T
    • [10]T: an array of Ts
    • struct { t T; u U ... }: a structure which contains a T as a component
    • ...

    The programmer can create complex data types by composing these simple rules. The total number of possible data types exceeds the number of useful data types. Clearly, there exist (and have to exist) data types which aren't useful at all. This is just a natural consequence of the fact that the rules for building new data types are simple.

    The type **T falls into the category of types which are less probable to appear in a program. The fact that it is possible to write *****T doesn't imply that such a type has to be immensely useful.


    And finally, the answer to your question:

    The type **T usually appears in contexts where we want to redirect users of a value of type T to another value of type T, but for some reason we do not have access to all users of the value or finding the users would cost too much time:

    1. We do not want to copy values of type T (for some reason)
    2. We want all users of a value of type T to access the value via a pointer
    3. We want to quickly redirect all users of a particular value of type T to another value

    In such a situation, using **T is natural because it allows us to implement the 3rd step in O(1):

    type User_of_T struct {
      Value **T
    }
    
    // Redirect all users of a particular value of type T
    // to another value of type T.
    func (u *User_of_T) Redirect(t *T) {
      *(u.Value) = t
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题