如何将成员函数传递给另一个成员函数? [英] How to pass a member function to another member function?

查看:73
本文介绍了如何将成员函数传递给另一个成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是将 A 类的成员函数传递给 B 类的成员函数:

My problem is about passing a member function from a Class A, to a member function of a Class B:

我尝试过这样的事情:

typedef void (moteurGraphique::* f)(Sprite);
f draw =&moteurGraphique::drawSprite;
defaultScene.boucle(draw);

moteurGraphique 是一个类,moteurGraphique::drawSprite 是一个成员函数,

moteurGraphique is A class, moteurGraphique::drawSprite is A member function,

defaultScene是B类的一个实例,boucle是B类的成员函数.

defaultScene is an instance of B class, and boucle is B member function.

在 A 的成员函数中调用的所有内容:

All that is called in a member function of A:

void moteurGraphique::drawMyThings()

我尝试了不同的方法来做到这一点,对我来说那种方法似乎更合乎逻辑,但它行不通!我得到:

I tried different ways to do it, that one seems the more logical to me, but it won't work! I got:

Run-Time Check Failure #3 - The variable 'f' is being used without being initialized.

我认为我做错了什么,有人可以解释我的错误吗?

I think I am doing something wrong, can someone explain my mistake ?

推荐答案

C++11 方式:

using Function = std::function<void (Sprite)>;

void B::boucle(Function func);
...

A a;
B b;

b.boucle(std::bind(&A::drawSprite, &a, std::placeholders::_1));

这篇关于如何将成员函数传递给另一个成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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