TRT
Sign in

Core · Module 5 · Lesson 5 of 6

Friend Functions, Namespaces and Operator Overloading

Grant narrow access with friends, organize code with namespaces, and overload plus and increment with const-correct, idiomatic signatures.

Pro — members onlyAbout 11 minutes

By the end you can

  • Grant minimal access with friend functions and friend classes
  • Organize helpers in namespaces and resolve names with scope syntax
  • Overload binary and unary operators with const-correct signatures

Builds on Smart Pointers: unique_ptr, shared_ptr and weak_ptr.

01 · A question first

Distance d1 holds 5 feet 11 inches and d2 holds 2 feet 3 inches, yet d1 + d2 will not compile, print code reaches into private fields, and a second display function collides by name. How do you add natural syntax for your own types without opening every field or renaming half the program?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Why member syntax alone falls short
  • Member pointers: pointing at class members themselves
  • How narrow friendship grants access
  • How overloads select meaning
  • What it costs and where to draw lines
  • Where it is used

03 · Practice

Something wrong with this lesson? Report a problem