指向成员函数的指针 [英] Pointer to member function

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

问题描述

您好,



我正在使用随机算法开发优化模块。

我写了一个模板类,用于优化和另一个准备目标函数的类。然后我需要将目标函数传递给模板类。但是,我有编译它的问题。



我正在尝试使用std :: function而不是typedef。



我需要知道定义目标函数的最佳方法是什么,并将其作为要优化的成员函数传递给另一个类。



提前谢谢,

mmgh



什么我试过了:



我试过函数指针和函数包装器。

Hello,

I am developing an optimization module using stochastic algorithm.
I wrote a template class which dose the optimization and another class which prepares the objective function. Then I need to pass the objective function to the template class. However, I have problem to compile it.

I am trying to use std::function instead of typedef.

I need to know what is the best way to define an objective function and pass it to another class as a member function to be optimized.

Thank you in advance,
mmgh

What I have tried:

I have tried function pointer and function wrapper.

推荐答案

请看我对这个问题的评论。让我说明一下这个想法:使用OOP而不是函数指针。



为什么要将函数用作对象?因为你想创建一些抽象,在这种情况下,从函数实现中抽象出一些行为。它知道函数对输入和输出的作用,但是如何计算结果是委托到实现计算的某个单独定义的对象。这样,您可以将此不可知行为与函数执行的计算结合起来。你需要不同的对象:一个知道如何计算某些东西的函数,以及使用知道输入传递内容和如何使用输出的函数的代码。



这样,你可以隔离两个知识领域:1)如何计算输入的输出; 2)如何处理它。



我只调用了使用对象抽象,但是函数也是抽象的 - 来自输入的来源和输出方式用来。现在你可以用不同的组合来满足这些抽象:将不同的功能与不同的用户结合起来。



一种使用该功能的方法是两个传递一些可以计算函数的对象一些其他功能参数。这个概念被称为代表。这个想法被称为一等公民:

授权(编程) - 维基百科,免费的百科全书 [ ^ ] ,

一等公民 - 维基百科,免费百科全书 [ ^ ]。



在C ++中,没有委托和接口这样的概念,但我们可以创建它们。我可以创建一个扮演委托类型的界面:

Please see my comment to the question. Let me illustrate the idea: using OOP instead of function pointers.

Why would you use a function as an object? Because you want create some abstraction, in this case, some behavior abstracted from the function implementation. It "knows" what the function has on input and output, but how the result is calculated is delegated to some separately defined object where the calculation is implemented. This way, you can combine this agnostic behavior and the calculation performed by the function. You have to different object: a function which "knows" how to calculate something, and the code using the function which "knows" what to pass on input and how to use output.

This way, you isolate two domain of knowledge: 1) how to calculate output from input; 2) what to do with it.

I called only the using object "abstraction", but the function is also abstracted — from where input comes from and how the output is used. Now you can meet those abstractions in different combination: combine different functions with different users.

One way to use the function is two pass some object which can calculate the function to some other function parameter. This concept is called "delegates". And the idea is called "first-class citizen":
Delegation (programming) - Wikipedia, the free encyclopedia[^],
First-class citizen - Wikipedia, the free encyclopedia[^].

In C++, there are no such concepts as "delegate" and "interface", but we can create them. I can create an interface which plays the role of delegate type:
#include <string>
#include <vector>

// ...

class IComparer { // essentially, this is interface
public:
   virtual int Compare(std::string left, std::string right) = 0;
   // technically, it is not required, but in the approach I explain, all instance functions are 
   // pure virtual; there are no data members
};



现在,您可以创建相同界面的不同实现:


Now, you can create different implementations of the same interface:

class LengthComparer : public IComparer {
	virtual int Compare(std::string left, std::string right) {
		if (left.length == right.length)
			return 0;
		else if (left.length < right.length)
			return -1;
		else
			return +1;
	}
};

class NumericComparer : public IComparer {
	virtual int Compare(std::string left, std::string right) {
		int iLeft = std::stoi(left);
		int iRight = std::stoi(right);
		if (iLeft == iRight)
			return 0;
		else if (iLeft < iRight)
			return -1;
		else
			return +1;
	}
};



我们不会使用会员比较的实施直接类,只有 IComparer.Compare



现在,我们可以创建一些只知道的类接口

现在,您可以创建一些数据处理工具,它使用基于 IComparer 的某些排序,但不知道它的任何实现:


We are not going to use the member Compare of implementation classes directly, only IComparer.Compare.

Now, we can create some class which "knows" only the interface
Now, you can create some data processing facility which uses some sorting based on IComparer but is unaware of any of its implementations:

class SomeAbstraction {
public:
	void ProcessData(std::vector<std::string xmlns:std="#unknown"> data, IComparer* comparer) {
		//...
		int indexA, indexB;
		//...
		if (comparer->Compare(data[indexA], data[indexB]) > 0)
			DoSomething();
	}
private:
		void DoSomething() { /* ... */ };
	//...
};
</std::string>



现在,使用 SomeAbstraction 的代码可以提供不同的实现 IComparer ,因此在最终计算中结合了不同的抽象:


Now, the code using SomeAbstraction can supply different implementation of IComparer, thus combining different abstraction in ultimate calculation:

SomeAbstraction abstraction;
std::vector<std::string xmlns:std="#unknown"> data;
// ...
abstraction.ProcessData(data, new LengthComparer());
// ...
abstraction.ProcessData(data, new NumericComparer());



我希望你能理解。



-SA


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

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