Key Takeaways

Get a recap of all the concepts learned in this chapter.

In this chapter, we learned about const return types. We saw that simply returning const values doesn’t make much sense and in fact it might even hurt the performance.

  • Returning a const& is dangerous because it might result in a dangling reference and a segmentation fault.

  • We can return values with const& if we know that the returned object will be available as long as its reference will be used and a copy would be expensive.

  • const pointers are a bit more varied as both the pointer and the pointed value can be declared const. As we saw, the pointer’s constness is ignored for return types, so we should only return const T* pointers if we want to bring some constness to our return values.

Get hands-on with 1200+ tech skills courses.