Parameters by value...

J

Johann

Hi all. I've read that in C++ it's not possible to pass a complete
block of memory by value as a parameter to a function. This is true in
arrays. But i can do it with a vector. What's the true then?

Another question: the implementantion of references it's like a const
pointer ??

Thanks in advance! and excuse my very poor english.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Johann said:
Hi all. I've read that in C++ it's not possible to pass a complete
block of memory by value as a parameter to a function. This is true in
arrays. But i can do it with a vector. What's the true then?

The truth is that standard C++ does not define "a complete block of memory".

You can't pass an array by value just because the language does not have a
way to do it. C++ just inherited the C way of decaying to pointers, and
nobody seen an urgent reason to introduce a new syntax. This has nothing to
do with some taboo against passing blocks of memory. After all, a POD
struct is a block of memory, and you can pass it by value.
 
A

Alf P. Steinbach

* Johann:
Hi all. I've read that in C++ it's not possible to pass a complete
block of memory by value as a parameter to a function.

That is incorrect.

This is true in arrays.

No.

Possibly you're thinking of arrays decaying to pointers in various
situations, like

void foo( char const s[5] );
// Equivalent to void foo( char const* s )

but you can easily pass an array by value by encapsulating it in a struct:

struct S{ char elem[5]; };

void foo( S s );

and here the whole array is copied in a call of foo.

But i can do it with a vector.

Impossible to say for sure what you mean here.

What's the true then?

In C++ all argument passing, except pass by reference, is pass by value.
In the case exemplified by the first foo above, a pointer is passed by
value.

Another question: the implementantion of references it's like a const
pointer ??

That's a good conceptual model for the low-level aspects of references,
but references have properties different from pointers (e.g., you can
overload freestanding operator functions on reference arguments, but not
on pointer arguments).
 

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
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top