5.10. Local Classes

A function may use a local class to create objects that it needs. Local classes are similar to nested classes, except that local class definitions appear inside blocks and functions. The following function, for instance, creates Circle objects with a local class.

 enum color { red, green, blue }; // enumeration bool bright = true; // global variable void make_circle(double rad, color c) { static bool visible = true; // static variable bool bright = true; // local variable class Circle { // local class private: double radius; color cn; bool visibility; bool contrast; public: Circle(double r, color hue, bool vis = false, bool con = false) { radius = r; cn = hue; visibility = vis; contrast = con; } void appearance() { visibility ...

Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.