将子类实例化为父类,但调用子类的方法
我正在编写一个应用程序,需要开发一个引擎来处理数据,但该引擎必须根据客户的需求更换为另一个引擎。 由于每个客户的需求都非常不同,因此我希望将每个引擎与其他引擎分开,这样我们只能使用客户所需的引擎来交付应用程序。
所以我的VS解决方案有以下项目:App、Engine_Base、Engine_A、Engine_B 应用程序 = exe Engine_Base = 父类...编译为dll并通过projec属性添加到App的引用中 Engine_A 和 Engine_B 都是 Engine_Base 的子类,并且都编译为自己的 dll(Engine_A.dll、Engine_B.dll)。 它们不会添加到应用程序的引用中,因此它们不会在运行时加载,因为我们不想将两者都发送给所有客户。
根据客户的配置文件,我们决定加载哪个引擎:
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));
}
由于仅将 Engine_Base 添加到 C++ 项目的引用中,因此我们将 Engine__A 或 Engine_B 对象转换为父类型。
然后我们为引擎的线程执行设置事件,因为它们需要很长时间才能运行(需要处理大量数据):
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));
但这给了我一个:
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 am writing an application that required the development of an engine for handling data, but the engine had to be replaceable by another depending on the customers' need. Since each customer had very different needs, I wanted to make each engine separated from the others so we could only deliver the application with the engines the customer required.
So my VS Solution has the following projects: App, Engine_Base, Engine_A, Engine_B
App = the exe
Engine_Base = the parent class...compiles to a dll and is added to the references of the App through the projec properties
Engine_A and Engine_B are both child classes of Engine_Base and both compile to a dll of their own (Engine_A.dll, Engine_B.dll). They are not added to references of App so that they will not be loaded at runtime since we do not want to ship both to all our customers.
Based on the configuration file of the customer 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 is added to the references of the C++ project we cast our Engine__A or Engine_B objects to the parent type.
Then we set the Events for the threaded execution of our engines as they take a long time to run (lots 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 a:
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 realize that this has to do with inheritance, but I'm getting short on ideas of how to fix this.
Does anyone have ideas on how to fix this?
Is our approach an alright solution or should we have been looking at something else and done things differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:也许确保 Engine_A 等方法被标记为虚拟?
Edit: Maybe make sure Engine_A, etc. methods are marked virtual?