指向成员函数的指针。 [英] Pointers to member functions.

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

问题描述



我正在玩模拟,系统的不同部分,如

控制器或温度计或热质量,表示为类,

实例化,并像Tinkertoys一样连接在一起。我的想法起初是
,每个类都有一个像get_ouput()这样的成员函数。当

某个物体像热质量需要输入时,比如来自控制系统的热量,我的想法是将一个指向该物体的指针添加到内部

列表,比如


target.add_heat(& controller);


但我发现我想要一个选择会员的灵活性更小

作为输入功能。所以我想使用与指针类似的东西

到常规函数,比如


float(* pf)();

pf = controller.get_heat;

target.add_heat(pf);


但我一直在玩那个主题的变体而我不能得到

工作。


我可以用C ++做这种事情吗?


-

意识不仅意味着模糊,舒适,模糊的感觉。这意味着

对当前状况的明确了解。 - NBSR辐射安全

培训


I''m playing with a simulation where different parts of a system, like a
controller or a thermometer or a thermal mass, are represented as classes,
instantiated, and connected together like Tinkertoys. My thought at first
was that each class would have a member function like get_ouput(). When
some object like a thermal mass needs an input, like heat from a control
system, my thought was to add a pointer to that object to an internal
list, like

target.add_heat(&controller);

But I''ve discovered I want a little more flexibility to choose a member
function as the input. So I''d like to use something analogous to pointers
to regular functions, like

float (*pf)();
pf = controller.get_heat;
target.add_heat(pf);

But I''ve been playing with variations on that theme and I can''t get it to
work.

Can I do this sort of thing in C++?

--
"Awareness means not just a vague, comfortable, fuzzy feeling. It means
explicit knowledge of current conditions." -- NBSR Radiation Safety
Training

推荐答案

Gregory L. Hansen写道:
Gregory L. Hansen wrote:
I我正在玩模拟,系统的不同部分,如控制器或温度计或热质量,被表示为类,实例化,并像Tinkertoys一样连接在一起。我首先想到的是每个班级都有一个成员函数,比如get_ouput()。当像热质量这样的物体需要输入时,比如来自控制系统的热量,我的想法是将指向该物体的指针添加到内部列表中,例如

target.add_heat(& controller);

但我发现我想要更灵活地选择一个
成员函数作为输入。所以我想使用类似于指向常规函数的指针,比如

float(* pf)();
pf = controller.get_heat;
target.add_heat(pf);

但我一直在玩这个主题的变化,我无法工作。


你有什么特别不能上班的?

我能用C ++做这类事吗?
I''m playing with a simulation where different parts of a system, like
a controller or a thermometer or a thermal mass, are represented as
classes, instantiated, and connected together like Tinkertoys. My
thought at first was that each class would have a member function
like get_ouput(). When some object like a thermal mass needs an
input, like heat from a control system, my thought was to add a
pointer to that object to an internal list, like

target.add_heat(&controller);

But I''ve discovered I want a little more flexibility to choose a
member function as the input. So I''d like to use something analogous
to pointers to regular functions, like

float (*pf)();
pf = controller.get_heat;
target.add_heat(pf);

But I''ve been playing with variations on that theme and I can''t get
it to work.
What specifically can''t you get to work?
Can I do this sort of thing in C++?




这是一个很好的问题,但你为什么问我们?问题是

基本上是关于*你的*能力,所以,你能做到吗?如果你想要询问是否*可能*用C ++做,那么答案就是是。

C ++是一种通用语言,并且几乎没有任何东西可以用它来完成




V



It''s a good question, but why are you asking us? The question is
essentially about *your* ability, so, can *you* do it? If you wanted
to ask whether it''s *possible* to do in C++, then the answer is "yes".
C++ is a general purpose language, and there is almost nothing that
cannot be done with it.

V


Gregory L Hansen写道:
Gregory L. Hansen wrote:
我正在玩一个模拟,系统的不同部分,如
控制器或温度计或热质量,表示为类,实例化,并像Tinkertoys一样连接在一起。我的想法最初是每个类都有一个像get_ouput()这样的成员函数。当像热质量这样的物体需要输入时,比如来自控制系统的热量,我的想法是将指向该物体的指针添加到内部
列表中,例如

target.add_heat(& controller);

但我发现我想要一点灵活性来选择一个成员
作为输入。所以我想使用与常规函数类似的指针

float(* pf)();
pf = controller.get_heat;
target.add_heat(pf);

但我一直在玩这个主题的变体而我无法工作。
我可以用C ++做这类事吗?
I''m playing with a simulation where different parts of a system, like a
controller or a thermometer or a thermal mass, are represented as classes,
instantiated, and connected together like Tinkertoys. My thought at first
was that each class would have a member function like get_ouput(). When
some object like a thermal mass needs an input, like heat from a control
system, my thought was to add a pointer to that object to an internal
list, like

target.add_heat(&controller);

But I''ve discovered I want a little more flexibility to choose a member
function as the input. So I''d like to use something analogous to pointers
to regular functions, like

float (*pf)();
pf = controller.get_heat;
target.add_heat(pf);

But I''ve been playing with variations on that theme and I can''t get it to
work.

Can I do this sort of thing in C++?




我不这么认为,除非函数是

非成员功能。


您可能会阅读设计模式,元素/

可重复使用的面向对象软件一书。由Gamma,Helm,

Johnson和Vlissides。


也许是MVC(模型/视图/控制器)方法,或者

Mediator设计模式最能满足您的需求。


引用设计模式调解员部分:


< quote>

定义一个封装一组对象如何交互的对象。

Mediator促进松散通过保持对象明确地相互引用

,它可以让你独立地改变他们的互动



..

..

..

同事发送和接收来自Mediator对象的请求。

中介通过路由实现合作行为

请求相应的同事。

< / quote>


问候,

Larry


-

反垃圾邮件地址,将每个''X'改为''。''直接回复。



I don''t think so, unless the functions are
non-member functions.

You might read the book "Design Patterns, Elements of
Reusable Object-Oriented Software" by Gamma, Helm,
Johnson, and Vlissides.

Perhaps an MVC (Model/View/Controller) approach, or the
"Mediator" design pattern would best meet your needs.

To quote from the "Design Patterns" Mediator section:

<quote>
Define an object that encapsulates how a set of objects interact.
Mediator promotes loose coupling by keeping objects from referring
to each other explicitly, and it lets you vary their interaction
independently.
..
..
..
Colleagues send and receive requests from a Mediator object.
The mediator implements the cooperative behavior by routing
requests between the appropriate colleague(s).
</quote>

Regards,
Larry

--
Anti-spam address, change each ''X'' to ''.'' to reply directly.


Gregory L. Hansen写道:
Gregory L. Hansen wrote:
但我发现我想要更灵活地选择
成员函数作为输入。所以我想使用类似
指针的常规函数​​,比如

float(* pf)();
pf = controller.get_heat;
target.add_heat(pf);

但我一直在玩这个主题的变化,我不能得到
它上班。
我可以做吗?用C ++做这类事情?
But I''ve discovered I want a little more flexibility to choose a member function as the input. So I''d like to use something analogous to pointers to regular functions, like

float (*pf)();
pf = controller.get_heat;
target.add_heat(pf);

But I''ve been playing with variations on that theme and I can''t get it to work.
Can I do this sort of thing in C++?




试试这些链接:
http://www.parashift.com/c++-faq-lit...o-members.html
http://www.newty.de/fpt/index.html


:: A ::



Try these links:
http://www.parashift.com/c++-faq-lit...o-members.html
http://www.newty.de/fpt/index.html

::A::


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

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