Best practices for class instance variables as parameters

H. S. Teoh hsteoh at qfbox.info
Sat Sep 28 20:00:54 UTC 2024


On Sat, Sep 28, 2024 at 06:16:55PM +0000, Ian via Digitalmars-d-learn wrote:
> Hi,
> 
> I'm coming from C and some C++ so the way D stores class instance
> variables is new to me. If I'm not mistaken the basic unadorned
> instance variable is like a "hidden" pointer. So, when passing class
> instance variables to a function, what would be the point of passing a
> pointer or ref?

Passing a pointer or ref to a class variable allows the callee to modify
the reference (e.g., make it point to a different instance of the
class).

Passing a class instance by value passes a reference to it, so the
callee can still modify the instance, but they cannot change the
caller's reference to it.


[...]
> Now I'm unsure. When I pass a class instance to a function by value,
> I'm not creating a copy of the instance, am I?

No, because classes are by-reference types.


T

-- 
In theory, software is implemented according to the design that has been carefully worked out beforehand. In practice, design documents are written after the fact to describe the sorry mess that has gone on before.


More information about the Digitalmars-d-learn mailing list