Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: What's the ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 29 of 30 Topic 9763 of 9983
Post > Topic >>

Re: What's the point of passing parameter by value (vs. const ref)

by Tony Delroy <tony_in_da_uk@[EMAIL PROTECTED] > Jul 22, 2008 at 03:28 PM

On Jul 20, 5:37 am, Jorgen Grahn <grahn+n...@[EMAIL PROTECTED]
> wrote:
> On Tue,  8 Jul 2008 20:28:28 CST, Tony Delroy
<tony_in_da...@[EMAIL PROTECTED]
> wrote:
> > On Jul 3, 5:27 pm, "Jim Langston" <tazmas...@[EMAIL PROTECTED]
> wrote:
> >> "Martin T." <0xCDCDC...@[EMAIL PROTECTED]
> wrote in message
> >> > When writing new C++ code, what is the point of passing any (input)
> >> > parameter by value when passing by const reference will just work
as
> >> > well? (Even and especially PODs, I would not do it with a complex
type
> >> > anyway.)
> >> > (Given that in 90% of the code you will never want to modify the
> >> > parameter anyway.)
>
> >      typedef int File_Handle;
>
> >      void fn(File_Handle fh);                      // (A) OR
> >      void fn(const File_Handle& fh);               // (B) OR
> >      void fn(Traits<File_Handle>::Const_Param fh); // (C) ??
>
> > I would argue that (A) presents a maintenance issue - particularly if
> > the typedef is provided by a library header.  Consider a change to a
> > "class File_Handler" in which copy construction and/or destruction
> > have unwanted side effects (e.g. badly implemented RAII), or some
> > large data is added (cache, buffer, ...?).  The client code (A) might
> > keep compiling but not run as intended.
>
> Good point, but unfortunate example.  The word "handle" in
> "File_Handle" is intended to signal to the user "see this as some kind
> of pointer or reference to a File".  I think most people would expect
> to be able to handle it as if it was a void *, or an index into some
> array not available to the programmer.
>
> /Jorgen

Quite so, and in general real IT references to "file handles" serve
this purpose with respect to OS-managed data.  Still, consider
possible layering of convenience functions over this abstraction: a
programmer might want to "enrich" these handles with a class to, for
example, open a file and store the file handle, ensure the file handle
was closed in the destructor, sup****t OO-style operations etc..  The
merits can be argued, but experience shows such cl***** do often get
written, and it's not unlikely the class would be named after the
handle abstraction it wraps.  You obviously understand the issues, but
for the benefit of others: the danger in this case is only some
unintended file opens and closes and tem****ary use of an additional O/
S file handle resource (of which there are typically limited numbers),
but in some cases (e.g. when locks are involved), worse things like
deadlocks could occur....

Tony

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 30 Posts in Topic:
What's the point of passing parameter by value (vs. const ref)
"Martin T." <  2008-07-02 11:58:01 
Re: What's the point of passing parameter by value (vs. const re
"Jim Langston"   2008-07-03 02:27:25 
Re: What's the point of passing parameter by value (vs. const re
Eric Johnson <eric.eer  2008-07-03 02:28:17 
Re: What's the point of passing parameter by value (vs. const re
"Hakusa@[EMAIL PROTE  2008-07-03 02:33:08 
Re: What's the point of passing parameter by value (vs. const re
mtlung@[EMAIL PROTECTED]   2008-07-03 03:28:03 
Re: What's the point of passing parameter by value (vs. const re
werasm <werasm@[EMAIL   2008-07-03 03:28:02 
Re: What's the point of passing parameter by value (vs. const re
=?UTF-8?B?RXJpayBXaWtzdHL  2008-07-03 03:28:01 
Re: What's the point of passing parameter by value (vs. const re
Le Chaud Lapin <jaibud  2008-07-03 03:28:02 
Re: What's the point of passing parameter by value (vs. const re
red floyd <redfloyd@[E  2008-07-03 03:28:02 
Re: What's the point of passing parameter by value (vs. const re
Pavel Minaev <int19h@[  2008-07-03 03:28:03 
Re: What's the point of passing parameter by value (vs. const re
Marcin.Barczynski@[EMAIL   2008-07-03 08:51:53 
Re: What's the point of passing parameter by value (vs. const re
Francis Glassborow <fr  2008-07-03 08:56:04 
Re: What's the point of passing parameter by value (vs. const re
Michael DOUBEZ <michae  2008-07-03 08:52:44 
Re: What's the point of passing parameter by value (vs. const re
"Andrew Koenig"  2008-07-03 09:58:01 
Re: What's the point of passing parameter by value (vs. const re
JohnMcG <JohnMcG@[EMAI  2008-07-03 15:45:38 
Re: What's the point of passing parameter by value (vs. const re
Mathias Gaunard <loufo  2008-07-03 15:47:07 
Re: What's the point of passing parameter by value (vs. const re
Deane Yang <deane.yang  2008-07-03 15:45:13 
Re: What's the point of passing parameter by value (vs. const re
"Jacky Goyon" &  2008-07-04 19:25:39 
Re: What's the point of passing parameter by value (vs. const re
"Martin T." <  2008-07-05 03:58:01 
Re: What's the point of passing parameter by value (vs. const re
Martin Bonner <martinf  2008-07-05 04:58:04 
Re: What's the point of passing parameter by value (vs. const re
David Abrahams <dave@[  2008-07-06 16:33:35 
Re: What's the point of passing parameter by value (vs. const re
LR <lruss@[EMAIL PROTE  2008-07-07 06:21:25 
Re: What's the point of passing parameter by value (vs. const re
Jorgen Grahn <grahn+nn  2008-07-07 17:36:35 
Re: What's the point of passing parameter by value (vs. const re
David Abrahams <dave@[  2008-07-07 17:57:14 
Re: What's the point of passing parameter by value (vs. const re
Le Chaud Lapin <jaibud  2008-07-08 15:23:54 
Re: What's the point of passing parameter by value (vs. const re
Tony Delroy <tony_in_d  2008-07-08 20:28:28 
Re: What's the point of passing parameter by value (vs. const re
Jorgen Grahn <grahn+nn  2008-07-19 14:37:18 
Re: What's the point of passing parameter by value (vs. const re
Jorgen Grahn <grahn+nn  2008-07-19 14:37:00 
Re: What's the point of passing parameter by value (vs. const re
Tony Delroy <tony_in_d  2008-07-22 15:28:24 
Re: What's the point of passing parameter by value (vs. const re
"Matthias Hofmann&qu  2008-07-24 13:58:15 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Sep 6 15:35:36 CDT 2008.