类模版有关问题
类模版问题
请教高手,我有200个菜单,有200个不同业务的窗体FORM,每个菜单的事件都是类似窗体创建\显示\释放的过程.如
fun0()
{
frmBusiness=new TfrmBusiness(Application);
frmBusiness-> ShowModal();
delete frmBusiness;
frmBusiness == NULL;
}
fun1()
{
frmBusiness1=new TfrmBusiness1(Application);
frmBusiness1-> ShowModal();
delete frmBusiness1;
frmBusiness == NULL;
}
...
我发现这些代码有相同的地方,想写一个通用的方法ShowForm(class T),里边包括了上面的4句话,菜单事件中只需要写一句话就行了。如
fun0()
{
ShowForm(TfrmBusiness)
}
fun1()
{
ShowForm(TfrmBusiness1)
}
...
这个ShowForm现在不会写,想到了类模板,但是好像对不上,请高手指教。
------解决方案--------------------
试下下面的代码
//模板
template <class T>
void CreateForm()
{
T *Form = new T(NULL);
Form-> ShowModal();
delete Form;
}
CreateForm <TForm2> (); //调用
请教高手,我有200个菜单,有200个不同业务的窗体FORM,每个菜单的事件都是类似窗体创建\显示\释放的过程.如
fun0()
{
frmBusiness=new TfrmBusiness(Application);
frmBusiness-> ShowModal();
delete frmBusiness;
frmBusiness == NULL;
}
fun1()
{
frmBusiness1=new TfrmBusiness1(Application);
frmBusiness1-> ShowModal();
delete frmBusiness1;
frmBusiness == NULL;
}
...
我发现这些代码有相同的地方,想写一个通用的方法ShowForm(class T),里边包括了上面的4句话,菜单事件中只需要写一句话就行了。如
fun0()
{
ShowForm(TfrmBusiness)
}
fun1()
{
ShowForm(TfrmBusiness1)
}
...
这个ShowForm现在不会写,想到了类模板,但是好像对不上,请高手指教。
------解决方案--------------------
试下下面的代码
//模板
template <class T>
void CreateForm()
{
T *Form = new T(NULL);
Form-> ShowModal();
delete Form;
}
CreateForm <TForm2> (); //调用