Connecting Tech Pros Worldwide Forums | Help | Site Map

const cl& vs cl

Michael
Guest
 
Posts: n/a
#1: Jul 22 '05
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



Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 22 '05

re: const cl& vs cl


* Michael:[color=blue]
> 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??[/color]

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...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
David T. Croft, Ph.D.
Guest
 
Posts: n/a
#3: Jul 22 '05

re: const cl& vs cl


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).



"Michael" <slick_mick_00@hotmail.com> wrote in message
news:co4oe8$k31$1@hercules.btinternet.com...[color=blue]
> 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
>
>[/color]


Alf P. Steinbach
Guest
 
Posts: n/a
#4: Jul 22 '05

re: const cl& vs cl


* David T. Croft, Ph.D.:[color=blue]
> [top-posting, trolling][/color]

Troll, go away.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Karl Heinz Buchegger
Guest
 
Posts: n/a
#5: Jul 22 '05

re: const cl& vs cl


"David T. Croft, Ph.D." wrote:[color=blue]
>
> One strength of Fortran is that all parameters are copied by reference. Why
> C passes by value is a mystery to me.[/color]

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?
*/

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Prof. Robert C. Wilson, Ph.D.
Guest
 
Posts: n/a
#6: Jul 22 '05

re: const cl& vs cl


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++.



beliavsky@aol.com
Guest
 
Posts: n/a
#7: Jul 22 '05

re: const cl& vs cl


Karl Heinz Buchegger <kbuchegg@gascad.at> wrote in message news:<41A5F86E.275CD611@gascad.at>...[color=blue]
> "David T. Croft, Ph.D." wrote:[color=green]
> >
> > One strength of Fortran is that all parameters are copied by reference. Why
> > C passes by value is a mystery to me.[/color]
>
> 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?
> */[/color]

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.
beliavsky@aol.com
Guest
 
Posts: n/a
#8: Jul 22 '05

re: const cl& vs cl


"Prof. Robert C. Wilson, Ph.D." <a@b.c> wrote in message news:<co56rt$gh2$1@opal.futuro.pl>...[color=blue]
> The main weakness of Fortran is that it has so little users, and this number
> is (fortunately) quickly going to zero.[/color]

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."
Arijit
Guest
 
Posts: n/a
#9: Jul 22 '05

re: const cl& vs cl


Prof. Robert C. Wilson, Ph.D. wrote:[color=blue]
> 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++.
>[/color]

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
David T. Croft, Ph.D.
Guest
 
Posts: n/a
#10: Jul 22 '05

re: const cl& vs cl



"Prof. Robert C. Wilson, Ph.D." <a@b.c> wrote in message
news:co56rt$gh2$1@opal.futuro.pl...[color=blue]
> 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++.[/color]

C# passes "value types" by value. Most objects are passed by reference. It
is a step closer to Fortran 90. Not there yet!
[color=blue]
>
>
>[/color]


Closed Thread