C++ Logo

sg20

Advanced search

Re: Introducing references

From: Yongwei Wu <wuyongwei_at_[hidden]>
Date: Thu, 27 Jan 2022 11:40:10 +0800
On Thu, 27 Jan 2022 at 04:03, Victor Eijkhout via SG20 <
sg20_at_[hidden]> wrote:

>
>
> On , 2022Jan26, at 13:33, Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
> wrote:
>
> FWIW, I teach
> - pass-by-value
> - problems: performance of all those copies; how to write an out-parameter?
> - C solution: pointers (a pointer holds a memory address)
> - pass-by-pointer
> - C++ enhancement: references (***)
> - pass-by-reference
>
>
> I agree with your sequence, and I use something very similar.
>
> But…..
>
> Why do you teach the “pass by pointer”?
>

My 2 cents here (in addition to Arthur’s good reply). I think there is a
use in daily programming to describe ‘potentially null’. Some people prefer
std::optional, but it has a bad performance impact, especially on large
objects, and it can only be used on an ‘in’ parameter, but not an ‘out’ or
‘in-out’ parameter. If there is no ownership involved and the argument can
be ‘missing’, I would recommend using a pointer (as versus a reference that
is not allowed to be null).

-- 
Yongwei Wu
URL: http://wyw.dcweb.cn/

Received on 2022-01-27 03:40:22