On 02/12/2021 22:42, James Harris wrote:
On 02/12/2021 20:49, Dmitry A. Kazakov wrote:
On 2021-12-02 21:31, James Harris wrote:
...
But to the point, are you comfortable with the idea of the A(2) in
x = A(2) + 0
meaning the same mapping result as the A(2) in
A(2) = 0
?
Yes, in both cases the result is the array element corresponding to
the index 2. That is the semantics of A(2).
Cool. If A were, instead, a function that, say, ended with
return v
then what would you want those A(2)s to mean and should they still mean
the same as each other? The latter expression would look strange to many.
Do you mean like returning a reference in C++ style?
int a[10];
void foo1(int i, int x) {
a[i] = x;
}
int& A(int i) {
return a[i];
}
void foo2(int i, int x) {
A(i) = x;
}
foo1 and foo2 do the same thing, and have the same code. Of course,
foo2 could add range checking, or offsets (for 1-based array), or have multiple parameters for multi-dimensional arrays, etc. And in practice
you'd make such functions methods of a class so that the class owns the
data, rather than having a single global source of the data.
On 03/12/2021 09:08, David Brown wrote:
On 02/12/2021 22:42, James Harris wrote:
On 02/12/2021 20:49, Dmitry A. Kazakov wrote:
On 2021-12-02 21:31, James Harris wrote:
...
But to the point, are you comfortable with the idea of the A(2) in
x = A(2) + 0
meaning the same mapping result as the A(2) in
A(2) = 0
?
Yes, in both cases the result is the array element corresponding to
the index 2. That is the semantics of A(2).
Cool. If A were, instead, a function that, say, ended with
return v
then what would you want those A(2)s to mean and should they still mean
the same as each other? The latter expression would look strange to
many.
Do you mean like returning a reference in C++ style?
Hi David, apologies for not replying before. I was just now looking for
old posts that were outstanding in some way. (There may be many which I
have yet to reply to like your one.)
I probably left your post until I found out about C++ references and
never got round to reading up on them.
int a[10];
void foo1(int i, int x) {
a[i] = x;
}
int& A(int i) {
return a[i];
}
void foo2(int i, int x) {
A(i) = x;
}
foo1 and foo2 do the same thing, and have the same code. Of course,
foo2 could add range checking, or offsets (for 1-based array), or have
multiple parameters for multi-dimensional arrays, etc. And in practice
you'd make such functions methods of a class so that the class owns the
data, rather than having a single global source of the data.
I see at https://www.geeksforgeeks.org/references-in-cpp/ code which includes
void swap(int& first, int& second)
{
int temp = first;
first = second;
second = temp;
}
That's not what I was thinking about. I don't care for it because in a
call such as
swap(a, b)
it's not clear in the syntax that the arguments a and b can be modified
- a calamity of a design, IMO. :-(
But in your case you are referring to a /returned/ value. That appears
to be OK except that to match what I had in mind I think there should be
a const in there somewhere. To illustrate, I was proposing that in a function, f, one could have
return h
which would /conceptually/ return the address of h (which I guess in
Algol terms means it would return h rather than the value of h).
Crucially, and perhaps at variance with Algol (I don't know) the value
of h (i.e. the value at the returned address) would be read-only to the caller.
The caller would be able to use the address returned as it could any
other address, but it could not write over the referenced value. If the callee returned with something like
return a[4]
then it would conceptually return the address of a[4] and, again, the
value at the returned address would be read-only in the caller.
What I've said so far is by default but overwriting would be possible.
To conceptually return the address of a variable which /could/ be overwritten one would use the rw modifier as in
return rw h
or
return rw a[4]
I'll say no more just now as this is an old topic but I wanted to at
least make a reply.
[...] To illustrate, I was
proposing that in a function, f, one could have
return h
which would /conceptually/ return the address of h (which I guess in
Algol terms means it would return h rather than the value of h).
Crucially, and perhaps at variance with Algol (I don't know) the
value of h (i.e. the value at the returned address) would be
read-only to the caller.
On 08/12/2023 20:28, James Harris wrote:
On 03/12/2021 09:08, David Brown wrote:
On 02/12/2021 22:42, James Harris wrote:
On 02/12/2021 20:49, Dmitry A. Kazakov wrote:
On 2021-12-02 21:31, James Harris wrote:
...
But to the point, are you comfortable with the idea of the A(2) in >>>>>>
x = A(2) + 0
meaning the same mapping result as the A(2) in
A(2) = 0
?
Yes, in both cases the result is the array element corresponding to
the index 2. That is the semantics of A(2).
Cool. If A were, instead, a function that, say, ended with
return v
then what would you want those A(2)s to mean and should they still mean >>>> the same as each other? The latter expression would look strange to
many.
Do you mean like returning a reference in C++ style?
Hi David, apologies for not replying before. I was just now looking
for old posts that were outstanding in some way. (There may be many
which I have yet to reply to like your one.)
I probably left your post until I found out about C++ references and
never got round to reading up on them.
I saw my post was dated 03.12 (or 12.03, for any date-backwards
Americans in the audience) and thought it was strange that I'd forgotten
the post from 5 days ago. Then I noticed the year...
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 917 |
Nodes: | 10 (1 / 9) |
Uptime: | 09:09:02 |
Calls: | 12,176 |
Calls today: | 6 |
Files: | 186,522 |
Messages: | 2,235,029 |