What is a handle in C++?

后端 未结 7 1195
眼角桃花
眼角桃花 2020-11-29 16:55

I have been told that a handle is sort of a pointer, but not, and that it allows you to keep a reference to an object, rather than the object itself. What is a more elaborat

7条回答
  •  隐瞒了意图╮
    2020-11-29 17:40

    This appears in the context of the Handle-Body-Idiom, also called Pimpl idiom. It allows one to keep the ABI (binary interface) of a library the same, by keeping actual data into another class object, which is merely referenced by a pointer held in an "handle" object, consisting of functions that delegate to that class "Body".

    It's also useful to enable constant time and exception safe swap of two objects. For this, merely the pointer pointing to the body object has to be swapped.

提交回复
热议问题