(Clarification) Object Pooling and Base Class Assignment( is this an Undefined Behavior?)

33 Views Asked by At

I'm trying to explore an old MMORPG Source to get a handle on how they "Used" to do things. I've come across a code that did something like this.

Note: This is not the actual code but I've written something that mimics the original Codes logic. EDIT: Note 2: the Pool Part of a Pool Class and I've not done this here to simplify things.

class VMAIN
{
public:
    int nVar1;
    int nVar2;
    VMAIN():nVar1(0),nVar2(0){;}
    virtual ~VMAIN (){;}
    void Reset()
    {
        nVar1=0;
        nVar2=0;
    }
    void Assign(const VMAIN& vSub)
    {
        *this = vSub;
    }
    VMAIN* GetMAIN(){return this;}
};
class VSUB: public VMAIN
{
public:
    float fVar1;
    float fVar2;
    VSUB():fVar1(0),fVar2(0){;}
    void Reset()
    {
        fVar1 = 0;
        fVar2 = 0;
    };
};
std::queue<VMAIN*> vecMain;
std::queue<VSUB*> vecSub;
VMAIN* NEW_VMAIN()
{
    VMAIN* pMain;
    if ( vecMain.empty() )
    {
        pMain = new VMAIN;
    }
    else
    {
        pMain = vecMain.front();
        vecMain.pop();
    }
    return pMain;
}
VSUB* NEW_SUB()
{
    VSUB* pMain = NULL;
    if ( vecSub.empty() )
    {
        pMain = new VSUB;
    }
    else
    {
        pMain = vecSub.front();
        vecMain.pop();
    }
    return pMain;
}
void RELEASE_MAIN(VMAIN* pMain)
{
    pMain->Reset();
    vecMain.push(pMain);
}
void RELEASE_SUB(VSUB* vSub)
{
    vSub->Reset();
    vecSub.push(vSub);
}
int main()
{
    std::queue<VSUB*>vSub;
    for ( int i = 0; i != 5 ; i++)
    {
        VMAIN* pMain = NEW_VMAIN();
        pMain->nVar1 = i+1;
        pMain->nVar2 = i+1;
        VSUB* pSub = NEW_SUB();
        pSub->Assign(*pMain);
        pSub->fVar1 = float(i)+0.5f;
        pSub->fVar2 = float(i)+0.5f;
        printf("Iteration: %d , nVar1 %d , nVar1 %d, fVar1 %.2f, fVar2 %.2f\n",i,pSub->nVar1,pSub->nVar2,pSub->fVar1,pSub->fVar2);
        vSub.push(pSub);
    }
    printf("\n Releasing \n \n");
    const int nSize = vSub.size();
    for ( int i = 0; i < nSize; i++)
    {
        VSUB* pSub = vSub.front();
        vSub.pop();
        printf("Iteration[Before]: %d , nVar1 %d , nVar1 %d, fVar1 %.2f, fVar2 %.2f\n",i,pSub->nVar1,pSub->nVar2,pSub->fVar1,pSub->fVar2);
        int j = i+10;
        VMAIN* pMain= NEW_VMAIN();
        pMain->nVar1 = j;
        pMain->nVar2 = j;
        if ( j  < 12 )
        {
            RELEASE_MAIN(pSub->GetMAIN());
            pSub->Assign(*pMain);
            printf("Iteration[After Replacing BASE]: %d , nVar1 %d , nVar1 %d, fVar1 %.2f, fVar2 %.2f\n",i,pSub->nVar1,pSub->nVar2,pSub->fVar1,pSub->fVar2);
        }
        else
        {
            RELEASE_MAIN(pMain);
            printf("Iteration[After Not Toching Base Values]: %d , nVar1 %d , nVar1 %d, fVar1 %.2f, fVar2 %.2f\n",i,pSub->nVar1,pSub->nVar2,pSub->fVar1,pSub->fVar2);
        }
        RELEASE_SUB(pSub);
        RELEASE_MAIN(pSub->GetMAIN());
        printf("Iteration[After Release]: %d , nVar1 %d , nVar1 %d, fVar1 %.2f, fVar2 %.2f\n\n",i,pSub->nVar1,pSub->nVar2,pSub->fVar1,pSub->fVar2);
    }
    int nSizeMain = vecMain.size(),nSizeSub = vecSub.size();
    printf("Size of vecMain %d, Size of vecSub %d\n",nSizeMain,nSizeSub);
}

The way I understood the code some values are calculated for the Base class and if it is acceptable it is assigned to the Derived class, and if not it's simply returned back to the pool. I tried the code above and it does work, but is it simply working by Relying on undefined behavior or is it something usually,or used to be practiced?

0

There are 0 best solutions below