chrise chrise - 1 year ago 72
C++ Question

c++ how to implement a switch between class members

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

main() {
shared_pointer<A> a = make_shared<A>();
if ( checkSomething ) {
} else {

class A {
doStuff() {

/*here I want to do something like call
m_b->doStuff() if this pointer is set and m_c->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

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?

Is there a way to use a static pointer so that I have a member

static **int m_switch;

and do something like

m_switch = condition ? &m_b : &m_c;

and call


Does the compiler here also replace the extra pointer hop because it is a static?

Is there any other smart way to do those switches?

Answer Source

Normally, class A would be an interface class, which both B and C would inherit and implement. But it sounds like you cannot do this for whatever reason.

Since you want to emulate this, you can start by making the interface:

class A_interface
    virtual void doStuff() = 0;
    virtual void doThings() = 0;
    virtual void doBeDoBeDo() = 0;

And then you make a template wrapper:

template< class T >
class A : public A_interface
   void doStuff() override { target.doStuff(); }
   void doThings() override { target.doThings(); }
   void doBeDoBeDo() override { target.doBeDoBeDo(); }

    T target;

This essentially does half of what your own example class A was trying to do, but now you can use a common interface. All you need to do is construct the correct templated version you want:

std::shared_ptr<A_interface> a;

if( checkSomething ) {
    a = std::make_shared<A<B>>();
} else {
    a = std::make_shared<A<C>>();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download