Return Value or Reference
Deciding how the value from a getter should be returned is important and requires some knowledge about the options available. In C++, we can return variables by value, by pointer, and by reference along with their const
counterparts, which we will discuss soon; however, we won't cover pointers in this chapter. The choice of how to return a variable largely depends on its use case, and this part of the chapter will cover this in the context of our Track
class, and specifically, how it applies to our getters and setters.
Return by Value
Look at the following getLength
method from the Track
class:
float getLength() { return m_lengthInSeconds; }
This is returning by value. In other words, this method returns a copy of the value of m_lengthInSeconds
. If this value is assigned to another variable, then any modifications to m_lengthInSeconds
will not be reflected in the new variable (and vice versa) since it was...