liveBook · Manning

Nonmember function

As explained in 100 C++ Mistakes and How to Avoid Them

A nonmember function is a C++ function defined outside a class and not belonging to any class instance.

A nonmember function is defined outside a class and does not belong to a class instance. In C++, functions may be members, nonmembers, or nonmember friends; choosing among them affects encapsulation and future maintenance. section 9.12

A formatting operation is often a good candidate for a nonmember. Keep the Date representation private, expose a small public interface, and let formatting call const accessors instead of coupling itself to stored fields. A friend can access private data directly, but friends should not alter object state. section 9.12

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Contrasts member, nonmember, and friend functions through Date formatting, but stops short of broader language mechanics.
Examples
Uses Date formatting, stream insertion, private date storage, and accessor-based output as concrete examples.
What it assumes
Assumes basic C++ classes, private data, member functions, friend functions, and stream insertion.
What you can do after
Evaluate whether formatting belongs outside a class and implement it through a stable public interface.
How it connects
Connects nonmember functions to encapsulation, public interfaces, friend functions, const accessors, and implementation hiding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.