Base class or derived class: Who should own a derived ptr (to a different class)
<p>Child1 is one of a dozen classes derived from BaseClass.  Bar1 is one of a dozen classes derived from TFoo.  All objects derived from BaseClass will have a pointer to an object derived from class TFoo.</p>

<p>QUESTION: which is considered better form:</p>

<p>1)  To have the base class own a pointer to a base class object (TFoo), and have
derived clases (Child1, Child2, etc.) downcast the TFoo pointer to the actual derived type they use and own</p>

<p>OR</p>

<p>2) To have the derived classes (Child1, Child2, etc.) own pointers to derived classes (Bar1, Bar2, etc.) and have a pure virtual function in the base class to get access to an upcast TFoo*?</p>

<p>(examples below are simplified -- not intended to be syntactically perfect) </p>

<h3>EXAMPLE 1:</h3>

<pre><code>class BaseClass
{
public:
    TFoo*   GetFoo(void)    { return mpFoo; }

protected:
    TFoo*   mpFoo;
};


class Child1 : public BaseClass
{
public:

    Bar1*   GetBar1(void)   { return (Bar1*)mpFoo; }
};


class Child2 : public BaseClass
{
public:
    Bar2*   GetBar2(void)   { return (Bar2*)mpFoo; }
};
</code></pre>

<h3>EXAMPLE 2:</h3>

<pre><code>class BaseClass
{
public:
    virtual TFoo*   GetFoo(void) = 0;
};


class Child1 : public BaseClass
{
public:

    virtual TFoo*   GetFoo(void)    { return (TFoo*) mpBar; }

    Bar1*   GetBar(void)    { return mpBar; }

protected:
    Bar1*   mpBar;
};


class Child2 : public BaseClass
{
public:

    virtual TFoo*   GetFoo(void)    { return (TFoo*) mpBar2; }

    Bar2*   GetBar(void)    { return mpBar2; }

protected:
    Bar2*   mpBar2;
};
</code></pre>
