title: C++ Best practice pattern of using polymorphic data member
body: <p>What is the best practice pattern of using polymorphic data member in C++?
  The simplest one that I know is just using a plain pointer. </p>
  
  <p>For example, if the type of data member is not polymorphic:</p>
  
  <pre><code>class Fruit {
    public:
      int GetWeight() { return 1; }
  };
  
  class Food {
    public:
      Food(Fruit f=Fruit()) : m_fruit(f) {}
      void SetFruit(Fruit f) { m_fruit = f; }
      Fruit GetFruit() { return m_fruit; }
      int Test() { return m_fruit.GetWeight(); }
    private:
      Fruit m_fruit;
  };
  // Use the Food and Fruit classes
  Food food1; //global scope
  void SomeFunction() {
    Fruit f;
    food1.SetFruit(f);
  }
  int main() {
    Fruit fruit1, fruit2;
    Food food2(fruit1);
    food2.SetFruit(fruit2);
    food2.SetFruit(Fruit());
    SomeFunction();
    food1.Test();
    food2.Test();
    return 0;
  }
  </code></pre>
  
  <p>It is easy to use the classes in many ways with minimum possibility for errors.</p>
  
  <p>Now if we make Fruit a base class (use polymorphic), this is the simplest/best one I can think of:</p>
  
  <pre><code>class Fruit {
    public:
      virtual int GetWeight() { return 0; }
  };
  
  class Apple : public Fruit {
    public:
      virtual int GetWeight() { return 2; }
  };
  
  class Banana : public Fruit {
    public:
      virtual int GetWeight() { return 3; }
  };
  
  class Food {
    public:
      Food(Fruit&amp; f=defaultFruit) : m_fruit(&amp;f) {}
      void SetFruit(Fruit&amp; f) { m_fruit = &amp;f; }
      Fruit&amp; GetFruit() { return *m_fruit; }
      int Test() { return m_fruit-&gt;GetWeight(); }
    private:
      Fruit* m_fruit;
      static const Fruit m_defaultFruit = Fruit();
  };
  // Use the Food and Fruit classes
  Food food1; //global scope
  void SomeFunction() {
    Apple a;
    food1.SetFruit(a);
  }
  
  int main() {
    Apple a;
    Banana b;
    Food food2(a);
    food2.SetFruit(b);
    food2.SetFruit(Apple()); //Invalid ?!
    SomeFunction(); //Weakness: the food1.m_fruit points to invalid object now.
    food1.Test();   //Can the compiler detect this error? Or, can we do something to assert m_fruit in Food::Test()?
    food2.Test();
    return 0;
  }
  </code></pre>
  
  <p>Is there any better way of doing this? I try to keep it simple not using any new/delete statement. (Note: must be able to have default value)</p>
  
  <p><strong>UPDATE:</strong>
  Lets consider a more practical example:</p>
  
  <pre><code>class Personality {
  public:
    void Action();
  };
  class Extrovert : public Personality {
    ..implementation..
  };
  class Introvert : public Personality {
    ..implementation..
  };
  
  class Person {
    ...
    void DoSomeAction() { personality-&gt;Action(); }
    ...
    Personality* m_personality;
  };
  </code></pre>
  
  <p>The aim is to have persons with different personalities, and to make it easy to add more Personality by subclassing. What is the best way to implement this? Since logically, we only need one object for each type of Personality, it does not make much sense for a Person to copy or assume ownership of Personality object. Could there be any difference approach/design that does not require us to deal with copy/ownership problem?</p>