1

I have the following classes, class A with a method, and a container class B:

class A
{
    void foo();
};
class B
{
    A * m_a;
    void setA(A* a)
    {
        m_a = a;
    }
    void callFoo()
    {
        m_a->foo();
    }
};

So now I want to extend the functionality of A to BarA adding fooBar(), so I also have to create a BarB to call this new method.

class BarA : public A
{
    void fooBar();
};


class BarB : public B
{
    void callFooBar()
    {
        // I know for sure its a BarA, but its saved as an A pointer
        BarA * barA = dynamic_cast<BarA*>(m_a)
        barA->fooBar();
    }
};

Now we create the caller:

class LetsFooBar
{
    BarA barA;
    BarB barB;

    void foobar()
    {
        barB.setA(&barA);
        barB.callFooBar();
    }
}

Everything works OK but there is the dynamic_cast issue in the pattern, as well as needing to extend the B class when wanting to extend the A class.

I would like to find a more elegant way to solve this issue.

Thank you

Hennio
  • 433
  • 2
  • 18

2 Answers2

1

If you don't like the dynamic_cast, you could store a BarA directly in BarB this way:

class BarB : public B
{
    BarA * m_bara;

    void setBarA(BarA* a) {
        m_bara = a;
    }

    void callFooBar() {
        m_bara->fooBar();
    }

    void callFoo() {
        m_bara->foo();
    }
};

Or this way, if you don't want override callFoo again

class BarB : public B
{
    BarA * m_bara;

    void setBarA(BarA* a) {
        m_bara = a;
        setA(a);
    }

    void callFooBar() {
        m_bara->fooBar();
    }
};

However, for a more elegant solution you have to be more specific on what you want to obtain.

eulerdisk
  • 4,299
  • 1
  • 22
  • 21
  • Thanks for the answer. Although this is a correct option too, I've marked @equality-7-2521 answer as correct because it suits better. The small issue here is that one will have a duplicate pointer to BarA and A in BarB class. Even thought they point to the same object, they have to be updated together whenever something changes. – Hennio Jul 08 '15 at 09:52
1

I agree with the other comments that the optimal solution is going to be tied to what you really want to do. However, here's another possibility that uses static polymorphism instead of dynamic polymorphism (including some small syntax changes to get it to compile):

class A
{
public:
    void foo();
};

template <typename T>
class B
{
protected:
    T * m_a;

public:
    void setA(T* a)
    {
        m_a = a;
    }

    void callFoo()
    {
        m_a->foo();
    }
};

class BarA : public A
{
public:
    void fooBar();
};

class BarB : public B<BarA>
{
public:
    void callFooBar()
    {
        m_a->fooBar();
    }
};

Note that this is just a slightly different take on Andrea's answer.

Rusty Shackleford
  • 1,111
  • 2
  • 14
  • 19
  • Thanks for the template tip, I finally implemented a mixin @πάνταῥεῖ. With the mixin pattern I can statically use polymorphism. As the typename of the internal object is set when creating the class, there is no need to dynamic cast. – Hennio Jul 08 '15 at 09:46