For the same reason you have to use in any other type of method. Or you should not use in any method. Let’s understand.
Data is passed to the parameter by value, then the argument value is copied to the variable that represents the parameter. Always.
What you can do is force a parameter to receive a pointer, probably creating a reference to the value you want to manipulate within the method. One of the ways to do this is to determine that the parameter is a reference, through the &
. Thus the reference is the value.
Nothing prevents you from creating the method without the reference, but copying all the data into the method can be very costly. This goes for any method without distinction.
In this case it may be interesting to use the reference since a string is passed by value and is very large, can generate a high cost in the passage of data.
Some compilers do optimizations for string
and avoid the cost of copying the entire structure. But you cannot count on this if the performance is important in any scenario. In new versions C++ uses a reference with semantics of move explicit and ensures optimization.
Now I return the question, why do you think you always have to do this? Whether read somewhere, taught wrong or did not understand the context.
Additional recommended readings:
If I’m not mistaken,
public class Pessoa
is not valid C++ ...– zentrunix