dimanche 14 mai 2023

Deleting dynamic_cast'ed pointer with multiple interfaces

I am trying to delete a dynamic_cast pointer which has multiple interfaces as shown in below program. By doing delete sb; , I am hoping to delete only resources of B, not other classes, but in my case it is deleting the other classes also.

Result:

   Inside B
   delete C
   delete B
   delete A
#include <iostream>
using namespace std;

enum ObjType {At =0, Bt, Ct};

//Interface A
struct A{
    virtual void do_A() = 0;
    virtual ~A(){
        cout<<"delete A"<<endl;
    }
};

//Interface B
struct B{
    virtual void do_B() = 0;
    virtual ~B(){
        cout<<"delete B"<<endl;
    }
};

//Interface C
struct C{
    virtual void do_C() = 0;
    virtual ~C(){
       cout<<"delete C"<<endl; 
    }
};

//Factory Server
struct FactoryServer: A, B, C
{
    void do_A(){
       cout<<"Inside A"<<endl;
    }
    void do_B(){
       cout<<"Inside B"<<endl;
    }
    void do_C(){
       cout<<"Inside C"<<endl;
    }
    
    static  FactoryServer* create(){
        return new FactoryServer();
    }

};

B* clientB(){
    return dynamic_cast<B*>(FactoryServer::create());
}

void clientAB(){
     
}

void clientBC(){
    
}

void clientCA(){
    
}

void clientABC(){
    
}

//client is allowed to takes services of any of A, B, and C or all of or some of 
int main(){
    
    B *sb = clientB();
    sb->do_B();
    delete sb;
    return 0;
}

Instead of deriving from all three interfaces, I can derive separately as required from any of, all of or some of interfaces, but that defeats the purpose of Factory.

I am not much familiar with Design Patterns, but in general how is the clean-up done in the scenarios like these.

Aucun commentaire:

Enregistrer un commentaire