Const classes as parameters
We have now discussed the default behavior when passing arrays to functions. The compiler will automatically pass in a pointer to the first element of the array. This is good for both speed and flexibility. Since only a pointer is passed in, the compiler doesn't need to spend time copying a (possibly) large array. This is also more flexible, because the function can work with arrays of all sizes instead of just a specific size.
Unfortunately, when passing structs or classes to a function, the default behavior is pass by value. We say unfortunately, because this will automatically evoke the copy constructor, which may be expensive and unnecessary if the function is only reading data from the data type. A good general ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access