c++ - c++中的方法类型

标签 c++ visual-studio-2008

我需要将方法作为参数发送(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/

相关文章:

visual-studio-2008 - 从 Visual Studio 构建时,让目标在运行其他任何东西之前运行

visual-studio-2008 - 使用 nant 和 VS2008 构建平台代码

c++ - 在opencv中拼接2张图片

visual-studio - 全局设置编译器标志(/Zm 内存限制)

c++ - 在 C++ dll 中查找外部调用

c++ - gen~ phasor() 是做什么的? (将 Max/MSP gen 转换为 C++)

visual-studio-2008 - Visual Studio 2008-获取断点之间的时间间隔?

调试期间的 Javascript 不是最新的

c++ - Boost::mutex 比没有 mutex 的程序花费的时间更少

c++ - 映射构造函数如何以类比较的形式使用?