Named Constructors for Clear Intent
Offer multiple ways to construct.
Why Named Constructors
One class often needs several ways to be built. A named constructor gives each path a clear label so callers know exactly what they get.
The Naming Syntax
Write the class name, a dot, then your label. The form ClassName.label reads like a tiny sentence at the call site.
class Point {
int x, y;
Point.origin() : x = 0, y = 0;
}All lessons in this course
- Default and Parameterized Constructors
- Named Constructors for Clear Intent
- Initializer Lists and Redirecting
- Factory and const Constructors