C ++调用指针到成员函数 [英] C++ Call Pointer To Member Function

查看:113
本文介绍了C ++调用指针到成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指向成员函数的指针列表,但是我很难尝试调用这些函数...什么是正确的语法?

I have a list of pointers to member functions but I am having a difficult time trying to call those functions... whats the proper syntax?

typedef void (Box::*HitTest) (int x, int y, int w, int h);

for (std::list<HitTest>::const_iterator i = hitTestList.begin(); i != hitTestList.end(); ++i)
{
    HitTest h = *i;
    (*h)(xPos, yPos, width, height);
}

也尝试在此添加成员函数

Also im trying to add member functions to it here

std::list<HitTest> list;

for (std::list<Box*>::const_iterator i = boxList.begin(); i != boxList.end(); ++i)
{
    Box * box = *i;
    list.push_back(&box->HitTest);
}


推荐答案

野兽具有独特的语法。

Pointers to members are a unique beast with unique syntax.

您必须有 Box 指针,将用作 this

You must have the Box pointer handy that will be used as this.

(box->*h)(xPos, yPos, width, height);

这篇关于C ++调用指针到成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆