Instantiating child class as parent, but calling child methods
I am writing an application that required the development of an engine for processing the data, but the engine had to be replaced with another depending on the needs of the clients. Since each client had very different needs, I wanted each engine to be separate from the others so that we could only deliver the application with the engines the client required.
So my VS Solution has the following projects: App, Engine_Base, Engine_A, Engine_B App = exe Engine_Base = parent class ... is compiled into dll and added to application links via projec properties Engine_A and Engine_B are child classes of Engine_Base and both are compiled into their own DLL (Engine_A.dll, Engine_B.dll). They are not added to the links to the application, so they will not be loaded at runtime as we do not want to send them to all our clients.
According to the client's config file, we decide which engine to load:
Engine_Base^ engine_for_app;
Assembly^ SampleAssembly;
Type^ engineType;
if (this->M_ENGINE == "A")
{
SampleAssembly = Assembly::LoadFrom("path\\Engine_A.dll");
engineType = SampleAssembly->GetType("Engine_A");
engine_for_app = static_cast<Engine_Base^>(Activator::CreateInstance(engineType, param1, param2));
}
else
{
SampleAssembly = Assembly::LoadFrom("path\\Engine_B.dll");
engineType = SampleAssembly->GetType("Engine_B");
engine_for_app = static_cast<Engine_Base^>(Activator::CreateInstance(engineType, param1, param2, param3, param4));
}
Since only Engine_Base references are added to the C ++ project references, we are casting our Engine__A or Engine_B objects into the parent type.
We then set events to thread our engines as they take a long time to run (a lot of data to process):
engine_for_app->OnComplete += gcnew CompleteEngineProcess(this, &frmMain::ThreadChildComplete);
engine_for_app->OnProgressInit += gcnew ProgressInitEngine(this, &frmMain::ThreadChildProgressInit);
engine_for_app->OnProgressReport += gcnew ProgressReportEngine(this, &frmMain::ThreadChildProgressReport);
Thread^ aThread;
aThread = gcnew Thread(gcnew ThreadStart(engine_for_app, &Engine_Base::Read));
But this gives me:
Error 2 error C3754: delegate constructor: member function 'Engine_A::Read' cannot be called on an instance of type 'Engine_Base ^' d:\_activeWork\EDI_TRUNK\src\App\frmMain.cpp 492
I understand that this has something to do with inheritance, but I don't understand how to fix this.
Does anyone have any ideas how to fix this?
Is our approach the right decision or should we look at something else and do something differently?
a source to share