const cl& vs cl

M

Michael

Hi, is there any time when passing by value would be better than passing by
const-reference? Surely if the recieving function wanted to modify the
passed in parametre for its own work, it would just create a copy of the
reference and would lose no performance but it would mean the .h file
wouldn't need to know about cl class to compile reducing header
dependances??

Mike
 
A

Alf P. Steinbach

* Michael:
Hi, is there any time when passing by value would be better than passing by
const-reference? Surely if the recieving function wanted to modify the
passed in parametre for its own work, it would just create a copy of the
reference and would lose no performance but it would mean the .h file
wouldn't need to know about cl class to compile reducing header
dependances??

Except for optimization a reference is, in practice, a pointer in
disguise.

That means it involves dereferencing, and allows the object's address
to be taken, and can be larger size than e.g. 'int'.

But generally these concerns do not weight nearly as much as the amount
of typing...
 
D

David T. Croft, Ph.D.

One strength of Fortran is that all parameters are copied by reference. Why
C passes by value is a mystery to me. The C problem has finally been fixed
in C# (which is more like Fortran).
 
K

Karl Heinz Buchegger

David T. Croft said:
One strength of Fortran is that all parameters are copied by reference. Why
C passes by value is a mystery to me.

Aha.
Then please tell me, what should happen in
(Assuming the default syntax should be pass per reference)

void foo( int i )
{
i = 6;
}

int main()
{
foo( 3 );

/* What now. foo assigned 6 to 3. Does that mean
that all 3's are 6 from now on?

Or should that be a syntax error, such that calling
foo with a constant literal is illegal at all?
*/
 
P

Prof. Robert C. Wilson, Ph.D.

The main weakness of Fortran is that it has so little users, and this number
is (fortunately) quickly going to zero.

Btw. C# also passes by value like C++.
 
B

beliavsky

Karl Heinz Buchegger said:
Aha.
Then please tell me, what should happen in
(Assuming the default syntax should be pass per reference)

void foo( int i )
{
i = 6;
}

int main()
{
foo( 3 );

/* What now. foo assigned 6 to 3. Does that mean
that all 3's are 6 from now on?

Or should that be a syntax error, such that calling
foo with a constant literal is illegal at all?
*/

In Fortran 90 and later versions, the INTENT (whether an argument is
allowed to be changed) of procedure arguments should be specified, and
procedures should be placed MODULEs to allow for interface checking.
The following code,

module twice_mod
contains
subroutine twice(x)
real, intent(in out) :: x
x = 2*x
end subroutine twice
end module twice_mod

program xintent
use twice_mod
call twice(1.0)
end program xintent

which tries to modify a constant, results in a compilation error with
g95 and other compilers:

In file xintent.f90:11

call twice(1.0)
1
Error: Argument for parameter 'x' at (1) is INTENT(INOUT) and actual
argument is not a variable

An advantage of Fortran is that one can pass arrays without dealing
with error-prone pointers.
 
B

beliavsky

Prof. Robert C. Wilson said:
The main weakness of Fortran is that it has so little users, and this number
is (fortunately) quickly going to zero.

They have been saying that for a long time. Fortran is still the
dominant language in areas such as quantum chemistry and numerical
weather prediction.
Quoting C++ expert Herb Sutter, "Fortran is alive and reasonably
well."
 
A

Arijit

Prof. Robert C. Wilson said:
The main weakness of Fortran is that it has so little users, and this number
is (fortunately) quickly going to zero.

Btw. C# also passes by value like C++.

Fortran has its uses. It probably isn't used as a mainstream language,
but it very widely used for numerically intensive computing. To give an
example from an area I work on, CFD simulations are almost exclusively
done in fortran.

-Arijit
 
D

David T. Croft, Ph.D.

Prof. Robert C. Wilson said:
The main weakness of Fortran is that it has so little users, and this
number is (fortunately) quickly going to zero.

Btw. C# also passes by value like C++.

C# passes "value types" by value. Most objects are passed by reference. It
is a step closer to Fortran 90. Not there yet!
 

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,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top