C ++成员函数指针 [英] C++ Member Function Pointers

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

问题描述

我在C ++做一个小游戏,我发现类成员函数指针。
我不想让他们以正确的方式工作,但这里是我的尝试。

  //一个结构体,其中的函数指针将被存储为调用
//顺便说一句,有一种方法来做类同样的事情吗?
//或者在C ++中结构仍然很好吗? (感觉像使用char而不是字符串)
typedef struct s_dEntitySpawn
{
std :: string name;
void(dEntity :: * ptr)();
} t_dEntitySpawn

//填充结构体,如果实体的类名是actor_basicnpc,
//然后我想做一个调用像ent-> spawnBasicNPC
t_dEntitySpawn dEntitySpawns [ ] = {
{actor_basicnpc,& dEntity :: spawnBasicNPC},
{0,0}
};

//这是每个实体被分析的地方
//和我调用函数指针
void dEntitiesManager :: spawnEntities()
{
实体*
t_dEntitySpawn * spawn;

[...]

//这里出错,对我来说很奇怪
if(!spawn-> name.compare - > getClassname()))
ent-> * spawn.ptr();

[...]
}

解决方案

/ div>

我认为你正在寻找的是

 (ent-> *(spawn- > ptr))(); 

让我们剖析一下。首先,我们需要得到实际的成员函数指针,它是

  spawn-> ptr 

由于 spawn 是一个指针,我们必须使用 - > 以选择 ptr 字段。



一旦我们这样做,我们需要使用指针到成员选择运算符来告诉 ent 来选择适当的成员函数:

  ent-> *(spawn-> ptr)


b $ b

最后,要调用函数,我们需要告诉C ++调用这个成员函数。由于C ++中的操作符优先级问题,您首先必须对整个表达式进行括号,以评估成员函数,因此我们有

  (ent-> *(spawn-> ptr))(); 

这是值得的,这是最古怪的C ++代码之一,我见过一会儿。 : - )



在一个完全无关的笔记上,因为你使用的是C ++,我会避免使用 typedef struct 。只需说

  struct t_dEntitySpawn {
std :: string name;
void(dEntity :: * ptr)();
};

希望这有助于!


I'm doing a little game in C++ and I'm discovering the class members function pointers. I don't have any idea to make them work in the right way, but here is my attempt.

// A struct where the function pointer will be stored for the call
// By the way, is there a way to do the same thing with classes ?
// or are structs still fine in C++ ? (Feels like using char instead of string)
typedef struct      s_dEntitySpawn
{
  std::string       name;
  void          (dEntity::*ptr)();
} t_dEntitySpawn;

// Filling the struct, if the entity's classname is "actor_basicnpc",
// then I would like to do a call like ent->spawnBasicNPC
t_dEntitySpawn      dEntitySpawns[] = {
  { "actor_basicnpc", &dEntity::spawnBasicNPC },
  { 0, 0 }
};

// This is where each entity is analyzed
// and where I call the function pointer
void            dEntitiesManager::spawnEntities()
{
  dEntity       *ent;
  t_dEntitySpawn    *spawn;

    [...]

      // It makes an error here, feels very weird for me
      if (!spawn->name.compare(ent->getClassname()))
        ent->*spawn.ptr();

    [...]
}

Could you please give me a good advice about the right way to implement them ?

Best regards.

解决方案

I think that the line you're looking for is

(ent->*(spawn->ptr))();

Let's dissect this. First, we need to get to the actual member function pointer, which is

spawn->ptr

Since, here, spawn is a pointer, and we have to use -> to select the ptr field.

Once we have that, we need to use the pointer-to-member-selection operator to tell ent to select the appropriate member function:

ent->*(spawn->ptr)

Finally, to call the function, we need to tell C++ to invoke this member function. Due to operator precedence issues in C++, you first have to parenthesize the entire expression that evaluates to the member function, so we have

(ent->*(spawn->ptr))();

For what it's worth, this is one of the weirdest lines of C++ code that I've seen in a while. :-)

On a totally unrelated note, because you're using C++, I would avoid using typedef struct. Just say

struct t_dEntitySpawn {
  std::string name;
  void (dEntity::*ptr)();
};

Hope this helps!

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

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