dimanche 27 mars 2016

Override super class member type with in subclass

I have a base class whose sub-classes need to be aware of specific subclass derived from another base class, however, I still want to be able to share common code rather than duplicate it. My solution for this was to pass a pointer back to the super class but I feel like there is a better way to do this.

Here is some example code to show what I am doing:

B.h

class B : public A
{
public:
    B();
}

Ax.h

class Ax
{
public:
    A *a;
    Ax(A *a);
{

Bx.h

class Bx : public Ax
{
public:
    B *b;
    Bx();
}       

Bx.cpp

Bx::Bx() : Ax(GetPointerToB())
{
}

B* Bx::GetPointerToB()
{
    if (!b) {
        b = new B();
    }

    return b;
}

Aucun commentaire:

Enregistrer un commentaire