如何将子项的方法作为参数传递给父项中的函数
how to pass method of child as parameter to function in parent
在以下非常简短的类中,我想在基中定义一个方法(ProcessLines),该方法将迭代一组数据库记录,将每个记录作为参数传递给仅在子类中定义的函数。 显然,Base 是一个虚拟类,永远不会自行实例化。
Class Base {
public:
typedef ProcLineFunc( Long *Line );
void ProcessLines( ProcLineFunc pf);
}
Class Child{
void DoWork( Long *Line) { //do something}
}
我不确定如何实现这一点。如果我在子项中重新声明 ProcessLines 并只调用父方法,则会收到与在创建子项的代码中调用 ProcessLines 相同的错误消息。
Child c(//something);
c.ProcessLines(c.DoWork);
给我一个编译器消息:
[BCC32 错误] 主.cpp(67): E2034 无法将"布尔值 (* (_closure )(长 *))(长)">转换为"整数"()(长 *)"
完整解析器上下文
主.cpp(56):类 Add2Chan
main.cpp(78): 决定实例化: bool Add2Chan::P rocessByLines()
--- 重置解析器上下文以进行实例化...
主.cpp(67): 解析: bool Add2Chan::P rocessByLines()
我对 c++ 相当陌生,E2034 错误消息吓坏了我。
请帮忙。我使用了一个typedef,这样我就可以在我的子类中多次调用ProcessLines,在我去的时候传递不同的函数。
通常你会用一个受保护的纯虚函数来做这种事情:
class Base {
public:
ProcessLines() {
//Logic to process lines here, obviously psuedo-code
while(moreLines) {
ProcessLine(line);
}
}
protected:
virtual void ProcessLine(const Line& line) = 0;
}
class Child : public Base {
protected:
void ProcessLine(const Line& line) { //Logic to process the line for this type }
};
class DifferentChild : public Base {
protected:
void ProcessLine(const Line& line) { //Logic to process the line for DifferentChild }
};
我认为这就是你要找的东西。在我看来,您似乎正在尝试以一种奇怪的方式实现多态性,但这是在C++中实现多态性的正常方法。
不要使用指向函数的指针,而是使用指向对象的指针。接受你的函数将被调用DoWork
而不是其他任何限制,并且每个类中只能有一个这样的函数。这不是一个糟糕的限制。在类(称为接口)中声明(纯虚拟)函数,并从中派生类(据说它们实现接口)。
struct DoingWork
{
virtual void DoWork(long *Line) = 0; // does some work on a list
};
struct DoingGreatWork: DoingWork
{
virtual void DoWork(long *Line) {printf("Great workn");}
};
struct DoingSlightWork: DoingWork
{
virtual void DoWork(long *Line) {printf("Slight workn");}
};
使用此示例:
class Base {
public:
void ProcessLines(DoingWork& object) {
//Logic to process lines here
while(moreLines) {
object.DoWork(line);
}
}
};
class Whatever // no need to derive from Base
{
void DoStuff()
{
Base object;
object.ProcessLines(DoingGreatWork());
object.ProcessLines(DoingSlightWork());
}
}
如果工作对象必须有权访问调用对象,请像这样初始化它们:
class Whatever // no need to derive from Base
{
struct DoingElaborateWork: DoingWork
{
Whatever& caller;
DoingElaborateWork(Whatever& caller): caller(caller) {}
virtual void DoWork(long *Line)
{
printf("Doing work requested by %sn", caller.name());
}
};
void DoStuff()
{
Base object;
object.ProcessLines(DoingElaborateWork(*this));
}
const char* name() {return "Whatever";}
}
附言他们说"在 C++03 中函数是二等公民",因为你不能用函数做你可以对对象做的事(比如我提供的这个解决方案)。我听说在 C++11 中功能得到了很大的改进,但我不确定细节。
由于您在 C++Builder 中执行此操作,因此您可以利用其__closure
扩展来执行您所要求的操作(VCL 的某些部分对自己的回调完全这样做):
class Base
{
public:
virtual ~Base() {}
typedef void (__closure *ProcLineFunc)( Long *Line );
void ProcessLines( ProcLineFunc pf);
};
class Child : public Base
{
public:
void DoWork( Long *Line) { //do something}
};
Child c(...);
c.ProcessLines(c.DoWork);
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 使用指向成员的指针将成员函数作为参数传递
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 如何传递带有通过引用传递的结构参数的函数?
- 如何在 c++ 中将函数作为参数传递?
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 为什么我不能将引用作为 std::async 的函数参数传递
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 将__device__ lambda 作为参数传递给 __global__ 函数
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 将函数作为参数传递以避免重复代码
- 如何在类(C )中作为参数传递函数