我需要将方法作为参数发送(MS Visual Studio 2008):
void Apply(Node<string>* node, void (visit(TreeEditor* self, Node<string> *)))
但是出现这个错误:
错误 C2664:“TreeEditor::Apply”:无法将参数 2 从“void (__thiscall TreeEditor::*)(TreeEditor *,Node *)”转换为“void (__cdecl *)(TreeEditor *,Node *)” d:\ed7\saod\labs\oopkkrtree\treeeditor\treeeditor.h 74
我尝试使用这种类型:
void Apply(Node<string>* node, void(__thiscall TreeEditor::*)(TreeEditor *,Node<string> *))
现在它可以工作了,但我不知道如何指定参数的名称(例如:void(func(int)))
我不能发送静态方法。
我试过这样做:
void Apply(Node<string>* node, void(visit)(__thiscall TreeEditor::*)(TreeEditor *,Node<string> *))
void Apply(Node<string>* node, void(visit(__thiscall TreeEditor::*)(TreeEditor *,Node<string> *)))
void Apply(Node<string>* node, void(__thiscall TreeEditor::*)(visit(TreeEditor *,Node<string> *)))
但是没有用。 请帮助我。
最佳答案
名称在 *
之后:
void Apply(Node<string>* node, void(TreeEditor::*visit)(TreeEditor *,Node<string> *))
__thiscall
是不必要的,它只适用于 Visual C++ (IIRC)。此外,您还需要传递或以其他方式使用 TreeEditor
来调用该方法。
我还建议为该回调类型提供一个 typedef
:
typedef void(TreeEditor::*TreeEditorVisitor)(TreeEditor*, Node<string>*);
然后你可以这样写Apply
:
void Apply(Node<string>* node, TreeEditorVisitor visit)
关于c++ - c++中的方法类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8362914/