Policy classes for making widgets I have been learning C++ for the past few weeks, and I made my first try with policy classes. Ultimately my goal is to use policy classes to manufacture wrapper classes on an std::vector. Part of what the wrapper class does is generate a custom allocator class that depends on the policy classes.

That's the inspiration for what I have below, which is a widget maker that makes widgets with a printing function that depends on the templated inputs.

Printing policy classes:

template &lt;class T&gt;
struct Chatty
{
  static void logVal(T val){
    std::cout &lt;&lt; val &lt;&lt; std::endl;
  }
  static void logS(std::string s){
    std::cout &lt;&lt; s &lt;&lt; std::endl;
  }
};

template &lt;class T&gt;
struct Silent
{
  static void logVal(T val){}
  static void logS(std::string s){}
};


Widget:

class Widget {
public:
  int x;
  int y;
  std::function&lt;void(std::string)&gt; print;
  Widget()
    :x(0), y(0), print([](std::string){})
  {};
};


WidgetManager:

template &lt; template &lt;class Created&gt; class CreationPolicy, template &lt;class Created&gt; class LoggingPolicy&gt;
class WidgetManager : public CreationPolicy&lt;Widget&gt;, public LoggingPolicy&lt;Widget&gt;
{
public:
  WidgetManager() {};
  static Widget* doAll(){
    Widget* w = WidgetManager::Create();
    std::function&lt;void(std::string)&gt; f1 = WidgetManager::logS;
    w-&gt;print = WidgetManager::logS;
    return w;
  }
};


I would appreciate feedback on all aspects of the code, but I also have two specific questions:


My biggest concern: is there a better way to assign functions to the Widgets I am making? For example, perhaps I could directly assign member functions of Widgets from my WidgetManager, but then I'd have to bind the WidgetMaker's static functions to individual instances of Widgets, which seems like more overhead. What's best performance-wise?
I am sure WidgetManager::doAll() is not the way to go, but I am struggling to find another way to call functions from various policies all together. What's a better way to do this?
In Widget, the members x and y should be private.  They should not be exposed to the interface in such a way.  You can either add the private keyword above them, or just move the public keyword below them (members are private by default, so both are essentially the same).