c++ how to implement a switch between class members
<p>I am very new to c++ so I am trying to get a feeling of how to do things the right way in c++. I am having a class that uses one of two members. which one gets determined at instantiation. It looks something like</p>

<pre><code>main() {
    shared_pointer&lt;A&gt; a = make_shared&lt;A&gt;(); 
    if ( checkSomething ) { 
        a-&gt;setB(make_shared&lt;B&gt;()); 
    } else {
        a-&gt;setC(make_shared&lt;C&gt;()); 
    }
    a-&gt;doStuff();

class A {
    public:
        doStuff() {

        /*here I want to do something like call 
        m_b-&gt;doStuff() if this pointer is set and m_c-&gt;doStuff() if 
        that pointer is set.*/

       }

        setB( B* p ) { m_b = p; }
        setC( C* p ) { m_c = p; }
        B* m_b;
        C* m_c;
    }
}
B and C are some classes with doStuff() member function
</code></pre>

<p>There are many members like doStuff. Ideally I would avoid checking for nullptr in each of them. What is the best/most efficient/fastest way to create a switch between those two members?</p>

<p>Is there a way to use a static pointer so that I have a member </p>

<pre><code>static **int m_switch;
</code></pre>

<p>and do something like </p>

<pre><code>m_switch = condition ? &amp;m_b : &amp;m_c;
</code></pre>

<p>and call </p>

<pre><code>*m_switch-&gt;doStuff();
</code></pre>

<p>Does the compiler here also replace the extra pointer hop because it is a static?</p>

<p>Is there any other smart way to do those switches?</p>