By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Handle?

P: n/a
What is exactly a handle to an object????Is it same as reference....

Jun 10 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Shraddha wrote:
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.
Jun 10 '07 #2

P: n/a
On 2007-06-10 06:20:01 -0700, Shraddha <sh*************@gmail.comsaid:
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.

--
Clark S. Cox III
cl*******@gmail.com

Jun 10 '07 #3

P: n/a
On Sun, 10 Jun 2007 09:43:51 -0400, Ron Natalie wrote:
>Shraddha wrote:
>What is exactly a handle to an object????Is it same as reference....
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).
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 10 '07 #4

P: n/a
On Jun 10, 3:49 pm, Clark Cox <clarkc...@gmail.comwrote:
On 2007-06-10 06:20:01 -0700, Shraddha <shraddhajosh...@gmail.comsaid:
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.
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #5

P: n/a
Roland Pibinger wrote:
On Sun, 10 Jun 2007 09:43:51 -0400, Ron Natalie wrote:
>>Shraddha wrote:
>>What is exactly a handle to an object????Is it same as reference....

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.
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.
>>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?
It seems quite common, but unneccesary in C++. It's more of a concept used
with C to compensate for the lack of classes.
>>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).
As I said, since it's not defined in the C++ standard, it might mean
different things.
Jun 11 '07 #6

P: n/a
On 2007-06-10 14:45:35 -0700, James Kanze <ja*********@gmail.comsaid:
On Jun 10, 3:49 pm, Clark Cox <clarkc...@gmail.comwrote:
>On 2007-06-10 06:20:01 -0700, Shraddha <shraddhajosh...@gmail.comsaid:
>>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.

The concept is well known in computer science in general. It's
an opaque identifier of something.
Which was one of the meanings in the list I provided (which you snipped)
(Handles existed way before Windows.)
I know this.
--
Clark S. Cox III
cl*******@gmail.com

Jun 11 '07 #7

P: n/a

"Shraddha" wrote:
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.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
triple-dubya dott tustinfreezone dott org
Jun 11 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.