What is the difference between: Handle, Pointer and Reference

后端 未结 4 1122
醉梦人生
醉梦人生 2021-01-30 07:22

How does a handle differ from a pointer to an object and also why can\'t we have a reference to a reference?

4条回答
  •  鱼传尺愫
    2021-01-30 08:00

    To even ask the question, "why can't we have a reference to a reference?" means you don't understand what a reference is.

    A reference is another name for an object; nothing more. If I have an object stored in variable X, I can create a variable Y that is a reference to this object. They're both talking about the same object, so what exactly would it mean to have a reference to Y? It wouldn't be any different from having a reference to X because they're all referencing the same thing.

    A "handle" does not have a definition as far as the C++ language is concerned. Generally speaking, a "handle" is a construct of some form which represents some sort of resource. You get it from some API that creates the resource. You call functions that take the handle as a parameter in order to query the state of the resource or modify it. And when you're done with it, you give it to some other API function.

    A pointer could be a handle. A reference could be a handle. An object could be a handle. An integer could be a handle. It all depends on what the system that implements the handle wants to do with it.

提交回复
热议问题