简体   繁体   English

指向C ++中的成员函数

[英]Pointers to member functions in C++

This is actually for a chess playing program, but the code is too long to post here so I'm going to use a simpler unrelated example: 这实际上是一个国际象棋游戏程序,但代码太长,无法发布在这里,所以我将使用一个更简单的无关示例:

Let's say I have an object like this: 假设我有一个这样的对象:

class A{
    int x1;
    int x2;
public:
    int Average(){ return (x1+x2)/2; }
};

I want to have a vector called AveragesList that stores all the averages (or a pointer to them) of all the x1 and x2 values of every object. 我想要一个名为AveragesList的向量,它存储每个对象的所有x1和x2值的所有平均值(或指向它们的指针)。 So I tried doing this: 所以我尝试这样做:

vector<int>* AveragesList;

class A{
    int x1;
    int x2;
public:
    int Average(){ return (x1+x2)/2; }
    A(){ AveragesList.push_back(this->Average); } //trying to add pointer to member function Average() to AveragesList
};

But when I try this, I get a message saying "A pointer to a bound function may only be used to call a function". 但是当我尝试这个时,我得到一条消息说“指向绑定函数的指针只能用于调用函数”。 Is there a work around? 有工作吗? I don't want to simply put value of the average of x1 and x2 in AveragesList, because if x1 or x2 changes, the value in AveragesList will not. 我不想简单地将x1和x2的平均值放在AveragesList中,因为如果x1或x2发生变化,则AveragesList中的值不会。 Also, my book said not to use public variables in C++ classes, so I'm not sure if I should use one. 另外,我的书说不要在C ++类中使用公共变量,所以我不确定是否应该使用它。

There is no built-in way of dealing with closures in C++ prior to C++11, so the easiest way of addressing the issue without using libraries such as boost would be as follows: define an integer member variable called average , in addition to your x1 and x2 variables. 在C ++ 11之前没有内置的处理C ++闭包的方法,因此在不使用boost等库的情况下解决问题的最简单方法如下:定义一个名为average的整数成员变量,除了你的x1x2变量。 Set average to the right value when you create your object, and update it every time the x1 or x2 is changed. 在创建对象时将平均值设置为正确的值,并在每次更改x1x2时更新它。 Store the pointer in the list, and use it to access the average. 将指针存储在列表中,并使用它来访问平均值。

This is not as good as calculating the result on the fly. 这不如在运行中计算结果那么好。 If you are using C++11, a better solution is available: 如果您使用的是C ++ 11,则可以使用更好的解决方案:

#include <iostream>
#include <vector>
#include <functional>

class A{
    int x1;
    int x2;
public:
    A(int _x1, int _x2) : x1(_x1), x2(_x2) {}
    int Average(){ return (x1+x2)/2; }
    void setX1(int _x1) { x1 = _x1; }
    void setX2(int _x2) { x2 = _x2; }
};

using namespace std;

int main() {
    vector<std::function<int()>> v;
    A a1(1, 5);
    A a2(2, 8);
    v.push_back([&]{return a1.Average();});
    v.push_back([&]{return a2.Average();});
    for (int i = 0 ; i != v.size() ; i++) {
        cout << v[i]() << endl;
    }
    a1.setX1(7);
    a2.setX2(32);
    for (int i = 0 ; i != v.size() ; i++) {
        cout << v[i]() << endl;
    }
    return 0;
}

If you don't want to store the current value of "average" ... then why not store a reference to "A"? 如果您不想存储“average”的当前值...那么为什么不存储对“A”的引用?

EXAMPLE: 例:

Class A{
    int x1;
    int x2;
public:
    A (int x1, int x2) { this->x1 = x1; this->x2 = x2; }
    int Average(){ return (x1+x2)/2 }
};

...

int
main (int argc, char *argv[])
{
  vector<A> averagesList;
  averagesList.push_back (new A(3, 4));
  ...

To do what (I'm guessing) you want to do, you also need to store the pointers of the objects that that you're going to use to call the functions: 要做你想做的事(我猜),你还需要存储你将要用来调用函数的对象的指针:

#include <vector>
#include <iostream>

class A {
  int x1,x2;
public:
  int Average(){ return (x1+x2)/2; }
  A(int a, int b);
};

std::vector< A* > objectVec;
std::vector< int (A::*)() > functionPtrVec;

A::A(int a, int b):
  x1(a), x2(b) {
  objectVec.push_back(this);
  functionPtrVec.push_back(& A::Average);
}

int main() {
  A a1(1,3);
  A a2(10,20);

  for (int k=0; k<objectVec.size(); k++) {
    int (A::* memberFuncPtr)() = functionPtrVec[k];
    A*object = objectVec[k];
    std::cout << (object->*memberFuncPtr)() << std::endl;
  }
  return 0;
}

You could use a pair to store them in the same vector , or define your own member functor class that stores both the object, the member function pointer, and has an () operator. 您可以使用一pair将它们存储在同一个vector ,或者定义您自己的成员functor类,它存储对象,成员函数指针,并且有一个()运算符。

HTH. HTH。

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

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