So which do you prefer for your method parameters that you modify,
address of or reference ?
Obj& Class1::operator=(const Obj* b) { /* code here */ }
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them. They
are a little more subtle than the dereference operator.
So which do you prefer for your method parameters that you modify,
address of or reference ?
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?ÿ They are not suitable for everything, but can be a good alternative to passing a pointer (or reference) to an output parameter.
Otherwise first consider if the parameter could be null.ÿ If that is possible, you need to use a pointer.
Then consider the clarity of the caller code.ÿ Passing by reference
looks a lot like passing by value.ÿ Indeed, if the function is not
modifying the parameter, prefer to pass by const reference if it is too
big (or otherwise expensive to copy) to pass conveniently by value.
If the function is called as "x = modify_this_thing(ys);", a reference
could be fine.ÿ If it is called as "x = make_a_thing(ys);", then it
looks like the parameter is being used by value - making the parameter a pointer so that the caller must use "&ys" makes it clearer that it might
be modified.
I don't think there is a single "right" answer here - think primarily
about how easy it would be for people to misunderstand what the code does.
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?ÿ They are not suitable for everything, but can be a good alternative to passing a pointer (or reference) to an output parameter.
Otherwise first consider if the parameter could be null.ÿ If that is possible, you need to use a pointer.
Then consider the clarity of the caller code.ÿ Passing by reference
looks a lot like passing by value.ÿ Indeed, if the function is not
modifying the parameter, prefer to pass by const reference if it is too
big (or otherwise expensive to copy) to pass conveniently by value.
If the function is called as "x = modify_this_thing(ys);", a reference
could be fine.ÿ If it is called as "x = make_a_thing(ys);", then it
looks like the parameter is being used by value - making the parameter a pointer so that the caller must use "&ys" makes it clearer that it might
be modified.
I don't think there is a single "right" answer here - think primarily
about how easy it would be for people to misunderstand what the code does.
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?
W dniu 5.06.2026 oÿ10:58, David Brown pisze:
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?
This is most stupid idea ever! In math valid function has the only one
value for each valid arg (even for very big and for very small numbers).
C lang authors follow this great design! Please do not break the root C
lang rules!
On 6/5/2026 3:58 AM, David Brown wrote:
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?ÿ They are not suitable for
everything, but can be a good alternative to passing a pointer (or
reference) to an output parameter.
Otherwise first consider if the parameter could be null.ÿ If that is
possible, you need to use a pointer.
Then consider the clarity of the caller code.ÿ Passing by reference
looks a lot like passing by value.ÿ Indeed, if the function is not
modifying the parameter, prefer to pass by const reference if it is
too big (or otherwise expensive to copy) to pass conveniently by value.
If the function is called as "x = modify_this_thing(ys);", a reference
could be fine.ÿ If it is called as "x = make_a_thing(ys);", then it
looks like the parameter is being used by value - making the parameter
a pointer so that the caller must use "&ys" makes it clearer that it
might be modified.
I don't think there is a single "right" answer here - think primarily
about how easy it would be for people to misunderstand what the code
does.
Yes, passing function parameters by reference is subtle.ÿ And that is
what I worry about.
I have been trying it in a few places to see if I like it.ÿ Such as:
ÿÿ int uniout5 (doublereal val [], const longint n, doublereal *sum, std::string & names, const longint key, const longint ksp);
W dniu 5.06.2026 oÿ10:58, David Brown pisze:
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?
This is most stupid idea ever!
In math valid function has the only one
value for each valid arg (even for very big and for very small numbers).
C lang authors follow this great design! Please do not break the root C
lang rules!
On 6/5/2026 1:58 AM, David Brown wrote:
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?ÿ They are not suitable for
everything, but can be a good alternative to passing a pointer (or
reference) to an output parameter.
Yeah. Fwiw, I was pondering on that for a function of mine for my field based DLA algo:
________________
ÿÿÿ ct::vector_field::vector_point const*
ÿÿÿ hit_test(
ÿÿÿÿÿÿÿ glm::vec2 const& p0,
ÿÿÿÿÿÿÿ float hit_eps
ÿÿÿ ) {
ÿÿÿÿÿÿÿ for (auto const& f0 : m_field.m_point_vec)
ÿÿÿÿÿÿÿ {
ÿÿÿÿÿÿÿÿÿÿÿ glm::vec2 pdif = p0 - f0.m_origin;
ÿÿÿÿÿÿÿÿÿÿÿ float pdis = glm::length(pdif);
ÿÿÿÿÿÿÿÿÿÿÿ if (pdis < hit_eps)
ÿÿÿÿÿÿÿÿÿÿÿ {
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ return &f0;
ÿÿÿÿÿÿÿÿÿÿÿ }
ÿÿÿÿÿÿÿ }
ÿÿÿÿÿÿÿ return nullptr;
ÿÿÿ }
________________
It returns a pointer to the field vector. So, its rather dangerous, but
I know how to use it fine. The point is that I am using a pointer to
gain the data _and_ show success and failure.
On Sat, 6 Jun 2026 15:28:01 +0200 =?UTF-8?B?8J+HtfCfh7FKYWNlayBNYXJjaW4gSmF3b3Jza2nwn4e18J+HsQ==?= gabbled:
W dniu 5.06.2026 oÿ10:58, David Brown pisze:
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?
This is most stupid idea ever! In math valid function has the only one
value for each valid arg (even for very big and for very small
numbers). C lang authors follow this great design! Please do not break
the root C lang rules!
A C function is not a mathematical function. Anyway , C++ still only ever returns a single object from a function but recent syntax updates allows it to seem as if multiple items are being returned.
eg: auto [a,b,c] = func();
If you have a problem with that bear in mind you've already been able to return a structure in C too.
On 06/06/2026 15:28, ??Jacek Marcin Jaworski?? wrote:
W dniu 5.06.2026 oÿ10:58, David Brown pisze:
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?
This is most stupid idea ever!
I'm pretty confident that I have had more stupid ideas on occasion.
In math valid function has the only one value for each valid arg (even
for very big and for very small numbers).
Did you do any maths beyond primary school?ÿ Vectors provide a
convenient way to hold multiple values together, and functions can map
to vectors - that corresponds very closely to C or C++ functions
returning a struct.ÿ (Mathematical functions can have any "type" for
their "return" - including other functions.)
And as has been pointed out already, C++ functions are not directly equivalent to maths functions, unless they are "pure" functions (such as
an [[unsequenced]] function in C23).
C lang authors follow this great design! Please do not break the root
C lang rules!
C++ is not C.
And even in C, struct returns are allowed - composing multiple sub-
values into one value.
W dniu 5.06.2026 oÿ10:58, David Brown pisze:
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?
This is most stupid idea ever! In math valid function has the only one
value for each valid arg (even for very big and for very small numbers).
C lang authors follow this great design! Please do not break the root C
lang rules!
On Sat, 6 Jun 2026 15:28:01 +0200 =?UTF-8?B?8J+HtfCfh7FKYWNlayBNYXJjaW4gSmF3b3Jza2nwn4e18J+HsQ==?= gabbled:
W dniu 5.06.2026 oÿ10:58, David Brown pisze:
On 05/06/2026 05:20, Lynn McGuire wrote:
So which do you prefer for your method parameters that you modify,
address of or reference ?
I am trying out the references and trying to decide if I like them.
They are a little more subtle than the dereference operator.
Have you considered multiple return values?
This is most stupid idea ever! In math valid function has the only one
value for each valid arg (even for very big and for very small
numbers). C lang authors follow this great design! Please do not break
the root C lang rules!
A C function is not a mathematical function. Anyway , C++ still only ever returns a single object from a function but recent syntax updates allows it to seem as if multiple items are being returned.
eg: auto [a,b,c] = func();
If you have a problem with that bear in mind you've already been able to return a structure in C too.
So which do you prefer for your method parameters that you modify,
address of or reference ?
We started writing our Windows user interface in the Windows 1.0 era
using plain old C.ÿ I converted it to C++ back in 2001 or so but I never used references since I just brought the C code mostly in.
On 6/6/2026 7:49 AM, boltar@caprica.universe wrote:
A C function is not a mathematical function. Anyway , C++ still only ever
returns a single object from a function but recent syntax updates allows it >> to seem as if multiple items are being returned.
Well, what about a function returning a single object with multiple ways
to examine the return state of the function?
Objecting to multiple return values because "math functions only have
one value" completely misses how actual computer hardware implements >activation records and calling conventions. It's all just data layout?
eg: auto [a,b,c] = func();
Sugar?
On Sat, 6 Jun 2026 12:25:49 -0700
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 6/6/2026 7:49 AM, boltar@caprica.universe wrote:
A C function is not a mathematical function. Anyway , C++ still only
ever
returns a single object from a function but recent syntax updates
allows it
to seem as if multiple items are being returned.
Well, what about a function returning a single object with multiple
ways to examine the return state of the function?
What about it?
Objecting to multiple return values because "math functions only have
one value" completely misses how actual computer hardware implements
activation records and calling conventions. It's all just data layout?
Dunno, it wasn't me objecting to it.
eg: auto [a,b,c] = func();
Sugar?
Of course, but then a lot of C++ is just sugared C, some of it useful, some of it not to much.
Am 05.06.26 um 05:20 schrieb Lynn McGuire:
So which do you prefer for your method parameters that you modify,
address of or reference ?
For obligatory parameters I prefer references.
There is also a slight performance benefit in case of multiple
inheritance (e.g. interfaces): since references must not be null the
null check for upcasts and downcasts can be omitted.
We started writing our Windows user interface in the Windows 1.0 era
using plain old C.ÿ I converted it to C++ back in 2001 or so but I
never used references since I just brought the C code mostly in.
Another dark chapter. Raw pointers are one of the most risky objects in
C++ code (with a few reasonable exceptions, of course). Basically they
are just an anachronism for compatibility reasons. C++ programmers that still prefer raw pointers are behind the times.
Marcel
There is also a slight performance benefit in case of multiple
inheritance (e.g. interfaces): since references must not be null
the null check for upcasts and downcasts can be omitted.
Another dark chapter. Raw pointers are one of the most risky objects in
C++ code (with a few reasonable exceptions, of course). Basically they
are just an anachronism for compatibility reasons. C++ programmers that still prefer raw pointers are behind the times.
Raw pointers are good for speed, speed, speed.
Am 09.06.2026 um 08:08 schrieb Lynn McGuire:
Raw pointers are good for speed, speed, speed.
Iterators have the same performance.
On 6/9/2026 1:56 AM, Bonita Montero wrote:
Am 09.06.2026 um 08:08 schrieb Lynn McGuire:
Raw pointers are good for speed, speed, speed.Iterators have the same performance.
I am talking about pointers going across function calls.
On 6/9/2026 1:56 AM, Bonita Montero wrote:
Am 09.06.2026 um 08:08 schrieb Lynn McGuire:
Raw pointers are good for speed, speed, speed.
Iterators have the same performance.
I am talking about pointers going across function calls.
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/9/2026 1:56 AM, Bonita Montero wrote:
Am 09.06.2026 um 08:08 schrieb Lynn McGuire:
Raw pointers are good for speed, speed, speed.Iterators have the same performance.
I am talking about pointers going across function calls.
How are pointers faster than references? In most cases, they're
likely to result in the same or very similar generated code.
I'm sure that's not always the case. Do you have examples?
On 6/9/2026 6:42 PM, Keith Thompson wrote:
Lynn McGuire <lynnmcguire5@gmail.com> writes:
On 6/9/2026 1:56 AM, Bonita Montero wrote:
Am 09.06.2026 um 08:08 schrieb Lynn McGuire:
Raw pointers are good for speed, speed, speed.Iterators have the same performance.
I am talking about pointers going across function calls.
How are pointers faster than references?ÿ In most cases, they're
likely to result in the same or very similar generated code.
I'm sure that's not always the case.ÿ Do you have examples?
Bonita was talking about iterators.ÿ I was talking about pointers versus references.ÿ I was trying to clarify things.
| Sysop: | Tetrazocine |
|---|---|
| Location: | Melbourne, VIC, Australia |
| Users: | 13 |
| Nodes: | 8 (0 / 8) |
| Uptime: | 29:34:10 |
| Calls: | 218 |
| Files: | 21,503 |
| Messages: | 84,138 |