|
Posted by rolkA on 09/30/07 10:58
On 30 sep, 12:47, Summercool <Summercooln...@gmail.com> wrote:
> I wonder which language allows you to change an argument's value?
> like:
>
> foo(&a) {
> a = 3
>
> }
>
> ...
> is there any way to prevent a function from changing the argument's
> value?
> ...
> Is there a way to prevent it from happening in the
> languages that allows it?
Hi,
Of course in C++, functions that don't modify argument's value should
(i'd rather say MUST) wait for a CONST reference or a value :
// const ref
foo(const T& a) {
a = 3; // error
}
// value
foo(T a) {
a = 3; // ok, but modify only the formal parameter : the argument
(aka actual parameter) is not changed
}
Now if you want to prevent a function (from a library you are using)
to modify it... Erm well, you shouldn't : a good librairy will never
wait for a non-const argument if it doesn't need to be modified. So in
this case "Is there a way to prevent it from happening" is
unpertinent : you could copy the object, but if the purpose of the
fucntion was to modify it, it's pointless.
[Back to original message]
|