我有两个几乎相同的表单(Form4和Form5),它们有几个公共项,但处理不同的数据。我正在尝试编写一个助手函数,它将采用以下两种形式之一。
这两个表单都是动态创建的。
到目前为止,我能够编写处理来自Form4进程(TForm4 *F)的数据的函数。我不能在Form5中这样做,因为助手函数是特定于TForm4的。
来自Form4
Edit1Exit(Tobject *Sender){
Process(this);
}来自Form5
Edit1Exit(Tobject *Sender){
Process(this);
}
Process(TForm4 *F){
// Do something like F->BitBtn1->Visible=false;
}问题是,Process( )是为TForm4编写的,因此它不接受TForm5。
如何声明Process(),使其采用任何一种形式。
发布于 2019-08-12 19:21:14
一般来说,您将有三种选择:
void Process(TForm4* F) {
/// do things
}
void Process(TForm5* F) {
/// do things
}class TFormBase {
// common virtual interface, and a virtual destructor
};
class TForm4 : public TFormBase {
// implementation of the interface + data members
};
class TForm5 : public TFormBase {
// implementation of the interface + data members
};
void Process(TFormBase* F) {
// interact with F via the virtual interface
}template<typename T>
void Process(T* F) {
// interact with the classes; assumes a common interface
}为了简单起见,我省略了许多细节,但这将使您开始工作。
https://stackoverflow.com/questions/57464415
复制相似问题