Here, we will look a bit into how arguments are passed into functions. You will find an example where everything is passed by value (by copy), but there is a struct that is slow to copy. We will try to work with it without copying it.
Check out functions.cpp
, compile it (make
) and run the program.
Check out Structs.h
. It defines two structs that we will work with : FastToCopy
and SlowToCopy
.
They are exactly what their name says, so let's try to avoid copying the latter.
Using printName()
as an example, write a function that prints the name of SlowToCopy
. Call it in main()
.
Try passing by copy and passing by reference, see the difference.
When passing by reference, ensure that your printName
cannot inadvertently modify the original object.
To test its const correctness, try adding something like argument.name = "a";
to your print function.
Try both with and without const attributes in your print function's signature.