[comp.lang.c++] Passing by reference

minar@reed.bitnet (Nelson Minar,L08,x640,7776519) (08/23/90)

I have one issue that concerns me about C++ syntax - the pass by reference
capabilities.

Lord knows its a convienience. It cleans the calling syntax up quite a bit,
it makes certain constructs possible that really aren't otherwise.

However, I find it really nice to know in C that 'hey, in this function call
that variable is passed by value. The function can't be changing the value
of that variable, and thus the bug is elsewhere.'  Its useful when debugging
other's arcane code.

In C++, this information is not available merely from the function call.
Who knows if

int a = 0;
foo(a);

will change the value of a or not in C++, without finding the prototype for
the function and looking carefully.


Is there any sort of nice convention to make this easier to deal with? I assume
not.

rogerson@PEDEV.Columbia.NCR.COM (Dale Rogerson) (08/24/90)

In article <15361@reed.UUCP> minar@reed.bitnet (Nelson Minar) writes:
>However, I find it really nice to know in C that 'hey, in this function call
>that variable is passed by value. The function can't be changing the value
>of that variable, and thus the bug is elsewhere.'  Its useful when debugging
>other's arcane code.

>In C++, this information is not available merely from the function call.
>Who knows if
>	int a = 0;
>	foo(a);
>will change the value of a or not in C++, without finding the prototype for
>the function and looking carefully.

>Is there any sort of nice convention to make this easier to deal with? I assume
>not.

Modula-2 (and Pascal) have always had passing by reference just specify the
parameter as VAR (i.e. foo( VAR a ) ).  I have never found this to be a 
problem in Modula-2.  However, Modula-2 has a very well defined idea of a
module.  Each module must have a Definition file and an Implementation file.
It is really easy to find out which file a function call is in and a quick
look at the Definition file will tell you what it modifies.  It is very 
quick and painless.  I have never been bitten by this, but I have been 
bitten by the missing & operator in C.

If you have well chosen filenames and classnames and provide good header 
files this should not be too bad in C++.  Remember that most(?) function calls
will probably be through a class pointer so finding the class should be
easy.  A nice solution would be to adopt the Modula-2 idea of Definition
and Implementation files which would cure many of the header file problems.
However, most C programmers are already accepting a suprising amount of 
Modula-2 ideas so I probably better not push my luck. :-)

The best solution is probably to get a good code browser which would get you
the function prototype at the press of a key. This requires no changes to
the language definition.  It might require coding practice changes.

None of these solutions will help for arcane code.



-----Dale
	Rogerson-----

roger@procase.UUCP (Roger H. Scott) (08/26/90)

In article <15361@reed.UUCP> minar@reed.bitnet (Nelson Minar) writes:
> ... the pass by reference capabilities.
>
>In C++, this information is not available merely from the function call.
>Who knows if
>
>int a = 0;
>foo(a);
>
>will change the value of a or not in C++, without finding the prototype for
>the function and looking carefully.
>
>
>Is there any sort of nice convention to make this easier to deal with? I assume
>not.
Yes and no.  The "convention" I use is to eschew use of "plain" reference
arguments and use only "const T&" (reference to *const* object) arguments as
a parameter passing efficiency optimization that has no semantic significance.
If I want a function to modify a value I'll damn well pass a pointer to the
value.
@
@
@
@
@
@
@
@
@
@
@

johnb@srchtec.UUCP (John Baldwin) (08/27/90)

In article <3233@PEDEV.Columbia.NCR.COM> rogerson@PEDEV.Columbia.NCR.COM
 (Dale Rogerson) writes:

>In article <15361@reed.UUCP> minar@reed.bitnet (Nelson Minar) writes:
>>... I find it really nice to know in C that ... The function can't be
>>changing the value of that variable ... 
>
>>Is there any sort of nice convention to make this easier to deal with?

>If you have well chosen filenames and classnames and provide good header 
>files this should not be too bad in C++.
> [more excerpted]
>
>The best solution is probably to get a good code browser which would get you
>the function prototype at the press of a key. This requires no changes to
>the language definition.  It might require coding practice changes.
>-----Dale Rogerson-----

Another practice which may help in debugging/understanding code, especially
interrelated code written by different authors, is the aggressive use of
the "const" modifier.  Not only does it inform the compiler about what your
intentions are (as far as what you do/do-not intend to modify), but we've
found it's very useful in letting other programmers know what other code
can or can't do (without having to wade through all the source code).

Another interesting effect (previously noted on this newsgroup) is that
once you start using "const,"  it starts percolating through all your
source and header files, as the compiler starts flagging places where
something you declared "const" could be changed.  Here, we are beginning
to find this has beneficial side effects.

-- 
John T. Baldwin                      |  johnb@srchtec.uucp
Search Technology, Inc.              |  johnb%srchtec.uucp@mathcs.emory.edu
standard disclaimer:                 | "... I had an infinite loop,
opinions and mistakes purely my own. |  but it was only for a little while..."