473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

handle/reference?

Specifically, is there any difference between usin

void MyFunction(const HANDLE myhandle

an

void MyFunction(const HANDLE& myhandleref

??
is the latter likely to cause problems (such as I have experienced with HANDLE pointers used in this way

Nov 17 '05 #1
13 2300
Bonj wrote:
Specifically, is there any difference between using

void MyFunction(const HANDLE myhandle)

and

void MyFunction(const HANDLE& myhandleref)

???
is the latter likely to cause problems (such as I have experienced
with HANDLE pointers used in this way)


Any function will cause problems when used incorrectly. Reference and
pointers are no exception. If you pass a handle by reference, you're
letting the called function change the handle value. If you pass it by
const reference or const, you're inhibiting such changes.

HANDLEs are value types - they should generally be passed by value, not
const, not reference, not pointer. Treat them as you would an int. Passing
them by reference just slows the code down, since you're adding a level of
indirection. Passing them by const value is pointless since changes within
the function can't "escape" anyway.

void MyFunction(HANDLE mh)

-cd
Nov 17 '05 #2
Right, thanks very muc
It's the 'level of indirection' thing I'm failing to consider when designing the code structure, but that's cleared it up
Nov 17 '05 #3
> HANDLEs are value types - they should generally be passed by value, not
const, not reference, not pointer. Treat them as you would an int.
Passing them by reference just slows the code down, since you're adding a
level of indirection.
To clarify what you've said:

It rather depends on how expensive it is to copy the object
you are passing, since you are in fact passing a copy. HANDLE is
likely to be pretty small - an int or similar - I can't remember.
In general, passing a const reference to a larger object is more
efficient.
Passing them by const value is pointless since changes
within the function can't "escape" anyway.


Indeed. Because they are copies.
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp

Nov 17 '05 #4
Carl Daniel [VC++ MVP] <cp*****************************@mvps.org.nospam > wrote:
[...] Passing them by const value is pointless [...]
Except that it might prevent bugs
within the function.
-cd


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #5
Hendrik Schober wrote:
Carl Daniel [VC++ MVP]
<cp*****************************@mvps.org.nospam > wrote:
[...] Passing them by const value is pointless [...]


Except that it might prevent bugs
within the function.


Sure. It'll make it illegal to assign to or otherwise modify the parameter,
so if you accidentally wrote the formal parameter name instead of some other
name on the left side of an assignment (etc), then the compiler would flag
it as an error. Still, I've never seen a coding guideline that suggested
that function parameters be const value types, but there's surely someone
out there using them that way for that reason.

-cd
Nov 17 '05 #6
Carl Daniel [VC++ MVP] wrote:
Sure. It'll make it illegal to assign to or otherwise modify the parameter,
so if you accidentally wrote the formal parameter name instead of some other
name on the left side of an assignment (etc), then the compiler would flag
it as an error. Still, I've never seen a coding guideline that suggested
that function parameters be const value types, but there's surely someone
out there using them that way for that reason.


I don't do it myself, but it's crossed my mind once or twice. :) I think
it's more justifiable than many guidelines that are commonly recommended. I
mean, if I have a local variable that I initialize in its declaration, and
that variable is never thereafter modified, I like to declare it const. It
seems odd that function parameters don't rate the same treatment, but no one
teaches it, and it seems odd and strangely wordy to practice it. That said,
it's merely an implementation detail to the function caller. It can be
hidden, since these declarations can be used interchangeably:

void f(int); // In header
void f(const int); // In source

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #7
Doug Harrison [MVP] <ds*@mvps.org> wrote:
[...]
I don't do it myself, but it's crossed my mind once or twice. :)
I have done it for a while. I stopped for
the reason given below.
I think
it's more justifiable than many guidelines that are commonly recommended. I
mean, if I have a local variable that I initialize in its declaration, and
that variable is never thereafter modified, I like to declare it const.
Good. It's not only me. :)
I wonder why this is done by so few. For
me it does catch errors at compile-time
that would go unnoticed otherwise.
It
seems odd that function parameters don't rate the same treatment, but no one
teaches it, and it seems odd and strangely wordy to practice it. That said,
it's merely an implementation detail to the function caller. It can be
hidden, since these declarations can be used interchangeably:

void f(int); // In header
void f(const int); // In source

Are you sure? I stopped doing this because I
found myself changing headers when suddenly
I needed some parameter to not to be 'const'.
And I considered changing headers because of
something that's an implementation detail a
bad thing.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #8
Hendrik Schober wrote:
these declarations can be used interchangeably:

void f(int); // In header
void f(const int); // In source


Are you sure?


Yep, they declare the same function.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #9
Doug Harrison [MVP] <ds*@mvps.org> wrote:
Hendrik Schober wrote:
these declarations can be used interchangeably:

void f(int); // In header
void f(const int); // In source


Are you sure?


Yep, they declare the same function.

Dang, I didn't know that!
However, I think this would confuse
people. So I won't use it.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #10
Hendrik Schober wrote:
Doug Harrison [MVP] <ds*@mvps.org> wrote:

Yep, they declare the same function.

Dang, I didn't know that!
However, I think this would confuse
people. So I won't use it.


FYI 8.3.5/3:

All declarations for a function with a given parameter list shall agree
exactly both in the type of the value returned and in the number and type of
parameters; the presence or absence of the ellipsis is considered part of
the function type.
....
After producing the list of parameter types, several transformations take
place upon these types to determine the function type. Any cv-qualifier
modifying a parameter type is deleted. [Example: the type void(*)(const int)
becomes void(*)(int) -end example] Such cv-qualifiers affect only the
definition of the parameter within the body of the function; they do not
affect the function type.

-cd
Nov 17 '05 #11
Carl Daniel [VC++ MVP] <cp*****************************@mvps.org.nospam > wrote:
[...]
FYI 8.3.5/3:

All declarations for a function with a given parameter list shall agree
exactly both in the type of the value returned and in the number and type of
parameters; the presence or absence of the ellipsis is considered part of
the function type.
...
After producing the list of parameter types, several transformations take
place upon these types to determine the function type. Any cv-qualifier
modifying a parameter type is deleted. [Example: the type void(*)(const int)
becomes void(*)(int) -end example] Such cv-qualifiers affect only the
definition of the parameter within the body of the function; they do not
affect the function type.
Mhmm.
In general, I don't pay much attention
to this legal stuff -- it goes far over
my head anyway. However, now that you
cited this: How does this say anything
about when it is reference or pointer
types?
There cv qualifier _are_ important.
-cd


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #12
Hendrik Schober wrote:
Carl Daniel [VC++ MVP]
<cp*****************************@mvps.org.nospam > wrote:
[...]
FYI 8.3.5/3:

All declarations for a function with a given parameter list shall
agree exactly both in the type of the value returned and in the
number and type of parameters; the presence or absence of the
ellipsis is considered part of the function type.
...
After producing the list of parameter types, several transformations
take place upon these types to determine the function type. Any
cv-qualifier modifying a parameter type is deleted. [Example: the
type void(*)(const int) becomes void(*)(int) -end example] Such
cv-qualifiers affect only the definition of the parameter within the
body of the function; they do not affect the function type.


Mhmm.
In general, I don't pay much attention
to this legal stuff -- it goes far over
my head anyway. However, now that you
cited this: How does this say anything
about when it is reference or pointer
types?
There cv qualifier _are_ important.


There the cv-qualifiers are not top level.

int const - top level CV
const int& - not top level CV (applies to int, not int&)
int * const - top level CV
const int * - not top level CV (applies to int, not int*)

Where the standard speaks of "cv-qualifiers modifying a parameter type" they
mean top-level cv-qualifiers. The 'const' in 'const int &' is modifying a
type that's embedded inside the type of the parameter, so the rule doesn't
apply.

-cd

Nov 17 '05 #13
Carl Daniel [VC++ MVP] <cp*****************************@mvps.org.nospam > wrote:
[...]
int const - top level CV
const int& - not top level CV (applies to int, not int&)
int * const - top level CV
const int * - not top level CV (applies to int, not int*)

Where the standard speaks of "cv-qualifiers modifying a parameter type" they
mean top-level cv-qualifiers. The 'const' in 'const int &' is modifying a
type that's embedded inside the type of the parameter, so the rule doesn't
apply.
I see. Thanks.
-cd


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: terminator800tlgm | last post by:
I don't understand the difference between ojbect handle and reference. For example, class Stack { private: CD cd_obj; // cd object DVD & dvd_ref;// dvd reference Why is it that dvd_ref can...
3
by: Christian Lammel | last post by:
i'm looking for a possibility in C# to get a unique Int32 handle for an object reference. This handle must later (within the same process of course) be convertible back to an object reference (call...
3
by: Fred Hebert | last post by:
I am trying to use a 3rd party DLL that requires the main window handle as a parameter. e.g. MyFunc(WHND MyHandle); The example looks something like this: Result = MyFunc(Handle); Where...
14
by: Howard | last post by:
Hi, I recently had a problem where I decided to store objects in a vector. (Previously, I had always stored pointers in vectors). Well, naturally, when storing an object in a vector, using...
2
by: Marko | last post by:
Hi, There are few properties that don't follow the traditional 'by handle' values. For example: System::Runtime::Serialization::Formatter ^f = ...;...
13
by: Abhishek | last post by:
Hi, how do I pass the handle of a control to the win32 api mouse_event. so that it will create the click event on that application only even if there is any other window in front of it. I dont...
5
by: RichG | last post by:
I'm looking for a way to bring an open form to the top. I know that is I open a form directly with form1.show() and then later, while the form is open I do another form1.show(), that I will get...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
7
by: Shraddha | last post by:
What is exactly a handle to an object????Is it same as reference....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.