简体   繁体   English

创建指向非静态类成员函数的类成员指针函数变量

[英]Creating a class member pointer function variable that points to a non-static class member function

The goal is to have the member variable _AddValue point to the CreateFirstValue function upon class initialization and after the first invocation of AddValue , all future calls to it will invoke CreateAnotherValue . 目标是让成员变量_AddValue在类初始化时指向CreateFirstValue函数,并在第一次调用AddValue ,以后对它的所有调用都将调用CreateAnotherValue

Previously, I just had a single AddValue function with a conditional check to determine which function to call. 以前,我只有一个带有条件检查的AddValue函数来确定要调用的函数。 However, I feel like that implementation is flawed because that if check will occur every time and it seems like a function pointer would be beneficial here. 但是,我觉得实现是有缺陷的,因为if每次都会进行检查,看起来函数指针在这里会很有用。

An example: 一个例子:

class Foo
{
 private:
  int _value;
  void (*_AddValue)(int value); // Pointer to function member variable

  void CreateFirstValue(int value)
  {
    _value = value;
    _AddValue = &CreateAnotherValue;
  }

  void CreateAnotherValue(int value)
  {
    // This function will create values differently.
    _value = ...;
  }

 public:
  // Constructor
  Foo()
   : _value(0), _AddValue(CreateFirstValue)
  {
  }

  AddValue(int value) // This function is called by the user.
  {
    _AddValue(value);
  }
};

The code above is not the actual code, just an example of what I'm trying to accomplish. 上面的代码不是实际的代码,只是我想要完成的一个例子。

right now I'm getting an error: argument of type void (BTree::)(int) does not match void (*)(int) 现在我收到一个错误: argument of type void (BTree::)(int) does not match void (*)(int)

 &CreateAnotherValue 

This syntax is not valid. 此语法无效。 To create a pointer-to-member, you have to name the class, even from inside other members. 要创建指向成员的指针,您必须为该类命名,甚至可以从其他成员内部命名。 Try 尝试

&Foo::CreateAnotherValue

In this case you are talking the address of a qualified non-static member function, which is allowed and prevents the error about address of unqualified member function. 在这种情况下,您正在讨论合格的非静态成员函数的地址,这是允许的并且可以防止关于不合格成员函数的地址的错误。

Of course, you then need an appropriately typed variable to store the pointer-to-member in, see Bo's answer for the correct declaration. 当然,您需要一个适当的类型变量来存储指向成员的指针,请参阅Bo的正确声明的答案。 When it comes time to call it, you will need to use the pointer-to-member-dereference operator (either .* or ->* ), so say 当需要调用它时,你需要使用指向成员的dereference运算符( .*->* ),所以说

(this->*_AddValue)(whatever);

The same rule applies to data, if you say &Foo::_value , you get a pointer-to-member of type int Foo::* . 同样的规则适用于数据,如果你说&Foo::_value ,你得到一个int Foo::*类型的指针成员。 But in the data case, the unqualified name is also accepted, but with very different behavior. 但在数据案例中,也接受了不合格的名称,但行为却截然不同。 &_value gives a normal pointer, type int* , which is the address of the specific _value member variable inside the this instance. &_value给出一个普通的指针,类型为int* ,它是this实例中特定_value成员变量的地址。

void (*_AddValue)(int value); void(* _AddValue)(int value); // Pointer to function member variable //指向函数成员变量的指针

This is not really a pointer-to-member, but a pointer to a free function. 这不是指向成员的指针,而是指向自由函数的指针。

You need to make this 你需要做到这一点

void (Foo::*_AddValue)(int value); // Pointer to function member variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM