Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a handle in C++?

Tags:

c++

handle

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 elaborate explanation?

like image 612
Xenoprimate Avatar asked Aug 19 '09 23:08

Xenoprimate


People also ask

What is handle in embedded C?

A handle is a reference to a system resource. Access to the resource is provided through the handle. However, the handle generally does not provide direct access to the resource. In contrast, a pointer contains the resource's address.

What is a handle to an object?

The term handle is used to mean any technique that lets you get to another object — a generalized pseudo-pointer. The term is (intentionally) ambiguous and vague. Ambiguity is actually an asset in certain cases.

What is handle data type?

The data type handle is useful when implementing native code to interact and exchange data with the target system. The handle literal can be null only to explicitly identify no reference to a system resource. The literal can be used within expressions wherever a handle operand is expected.

What is a handle pointer?

Handle pointers are instances of a type called a handle. You use a handle instead of an ordinary pointer to refer to a data structure. Handle pointers point to data in some external memory area, such as expanded memory, extended memory, or disk.


2 Answers

A handle can be anything from an integer index to a pointer to a resource in kernel space. The idea is that they provide an abstraction of a resource, so you don't need to know much about the resource itself to use it.

For instance, the HWND in the Win32 API is a handle for a Window. By itself it's useless: you can't glean any information from it. But pass it to the right API functions, and you can perform a wealth of different tricks with it. Internally you can think of the HWND as just an index into the GUI's table of windows (which may not necessarily be how it's implemented, but it makes the magic make sense).

EDIT: Not 100% certain what specifically you were asking in your question. This is mainly talking about pure C/C++.

like image 75
Matthew Iselin Avatar answered Sep 16 '22 15:09

Matthew Iselin


A handle is a pointer or index with no visible type attached to it. Usually you see something like:

 typedef void* HANDLE;  HANDLE myHandleToSomething = CreateSomething(); 

So in your code you just pass HANDLE around as an opaque value.

In the code that uses the object, it casts the pointer to a real structure type and uses it:

 int doSomething(HANDLE s, int a, int b) {      Something* something = reinterpret_cast<Something*>(s);      return something->doit(a, b);  } 

Or it uses it as an index to an array/vector:

 int doSomething(HANDLE s, int a, int b) {      int index = (int)s;      try {          Something& something = vecSomething[index];          return something.doit(a, b);      } catch (boundscheck& e) {          throw SomethingException(INVALID_HANDLE);      }  } 
like image 20
jmucchiello Avatar answered Sep 19 '22 15:09

jmucchiello