Handle?

R

Ron Natalie

Shraddha said:
What is exactly a handle to an object????Is it same as reference....
Handle is NOT a C++ term. In windows a HANDLE is an opaque datatype
that really was a pointer at some point (perhaps in the operating
systems address space) that you're not supposed to treat as a pointer
just an indentifier of some system created resource.

Think of it as a void* you can't do anything with other than pass
back to other functions in the API.
 
C

Clark Cox

What is exactly a handle to an object????Is it same as reference....

The term "handle" is meaningless as far as C++ is concerned. Different
platforms and environments use the term for different concepts. I have
seen handle to mean any of: A pointer, an opaque identifier, a
pointer-to-pointer, a smart pointer. Please refer to the documentation
for your specific environment for more information.
 
R

Roland Pibinger

Sort of. A handle typically encapsulates access to resources and/or
objects.
Handle is NOT a C++ term.

It is a term of any language, including C++, that uses handles. In C++
handles can take advantage of genuine C++ idioms like RAII. See
http://www.research.att.com/~bs/glossary.html#Ghandle
In windows a HANDLE is an opaque datatype
that really was a pointer at some point (perhaps in the operating
systems address space) that you're not supposed to treat as a pointer
just an indentifier of some system created resource.

Great idea, isn't it?
Think of it as a void*

A handle (and also a HANDLE) usually isn't a void*.
you can't do anything with other than pass
back to other functions in the API.

.... which is exactly what you want when you use a handle.
BTW, Stroustrup sometimes uses 'handle' synonymously with 'smart
pointer' which is, IMO, misleading ('smart pointers' don't encapsulate
the pointed-to objects).
 
J

James Kanze

On 2007-06-10 06:20:01 -0700, Shraddha <[email protected]> said:
The term "handle" is meaningless as far as C++ is concerned. Different
platforms and environments use the term for different concepts.

The concept is well known in computer science in general. It's
an opaque identifier of something. (Handles existed way before
Windows.)

As names go, it's pretty poor, because it is also often used as
a verb. If you don't know what else to name a function, call it
handle(). It's one of those names that, if you see it in a
program, you know that the design wasn't sufficiently precise.
So while it's usable as a noun, it lends to confusion, and it is
far better to use id or Identifier, or something along those
lines.
 
R

Rolf Magnus

Roland said:
Sort of. A handle typically encapsulates access to resources and/or
objects.


It is a term of any language, including C++, that uses handles.

The C++ language definiton doesn't define that term. Basically, things that
aren't defined in the C++ standard are off-topic here.
When using some non-standard libraries that use something called 'handle',
it might mean different things depending on the library.
Great idea, isn't it?

It seems quite common, but unneccesary in C++. It's more of a concept used
with C to compensate for the lack of classes.
A handle (and also a HANDLE) usually isn't a void*.


... which is exactly what you want when you use a handle.
BTW, Stroustrup sometimes uses 'handle' synonymously with 'smart
pointer' which is, IMO, misleading ('smart pointers' don't encapsulate
the pointed-to objects).

As I said, since it's not defined in the C++ standard, it might mean
different things.
 
R

Robbie Hatley

Shraddha said:
What is exactly a handle to an object?

There is no "exact" answer. "Handle" is not a C++ term.
It's usually used to mean an identifier for a dynamically-
allocated object. (Examples: windows, icons.) This is
in contrast to an "identifier" which usually refers to
statically-allocated objects. (Examples: controls in a
window template.)
Is it same as reference?

No. In C++ a "reference" is an "alias", or an alternate
name, for an object.

Example:

std::string & Name = status_object.IncomingFileRecord.name;

You can now write to status_object.IncomingFileRecord.name
like this:

Name = "qb_7309.icf";

Instead of having to write:

status_object.IncomingFileRecord.name = "qb_7309.icf";

Much neater!

(Not to mention passing arguments to functions by non-const
reference, which makes it a breeze to use one function to
alter variables in another function. VERY nice technique.)

I suppose a "handle" could be implimented using a reference,
but it usually isn't. In my compiler at work (CVI), handles
are ints. In another compiler I worked with (Visual C++),
handles are void*. In the compiler I use at home for making
system utilities (gcc) handles are not used. It varies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,293
Messages
2,571,505
Members
48,192
Latest member
LinwoodFol

Latest Threads

Top