By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Parameters by value...

P: n/a
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.

Nov 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Johann wrote:
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.

--
Salu2
Nov 16 '06 #2

P: n/a
* 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).

--
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?
Nov 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.