c++普通函数和成员函数作为参数传入另一个普通函数

普通函数接收另一个普通函数作为参数传递

method

//fun_ptr 作为 test_fun_ptr的第一个参数
Eigen::MatrixXd fun_ptr(Eigen::MatrixXd A, Eigen::MatrixXd B);
//注意下面这个函数第一个参数如何写的就行了:  返回类型(*f)(f接收的参数类型), 很 easy
void test_fun_ptr(Eigen::MatrixXd(*f)(Eigen::MatrixXd, Eigen::MatrixXd), Eigen::MatrixXd A, Eigen::MatrixXd B);

example

#include <Eigen/Dense>
#include <iostream>
#include <vector>
#include <tuple>
#include <set>
#include <map>

using namespace std;
using namespace Eigen;


pair<vector<pair<int, int> >, vector<vector<int> > > test_vector_l();

Eigen::MatrixXd fun_ptr(Eigen::MatrixXd A, Eigen::MatrixXd B);
//注意下面这个函数第一个参数如何写的就行了
void test_fun_ptr(Eigen::MatrixXd(*f)(Eigen::MatrixXd, Eigen::MatrixXd), Eigen::MatrixXd A, Eigen::MatrixXd B);


int main(){
    
    Eigen::Matrix3d a;
    a << 1, 0, 2, 
         3, 2, 1,
         0, 1, -1;

    Eigen::Matrix3d a2 = Eigen::Matrix3d::Identity(3, 3)*2;
    
    test_fun_ptr(fun_ptr, a, a2);

    return 0;

}


Eigen::MatrixXd fun_ptr(Eigen::MatrixXd A, Eigen::MatrixXd B){

    cout << "in fun_ptr: \n\n" << "matrix A:\n" << A << "\n\nMatrix B:\n" << B << endl;
    Eigen::Matrix3d cost = Eigen::Matrix3d::Zero(3, 3);  
    for (int i=0; i<A.rows(); ++i)
        for (int j=0; j<A.cols(); ++j)
            cost(i,j) = sqrt(pow(A(i,j) - B(i, j), 2));

    return cost;
}


void test_fun_ptr(Eigen::MatrixXd(*f)(Eigen::MatrixXd, Eigen::MatrixXd), Eigen::MatrixXd A, Eigen::MatrixXd B){
    MatrixXd output = f(A, B);

    cout << "\noutput:\n" << output << endl;

}

/* output
in fun_ptr: 

matrix A:
 1  0  2
 3  2  1
 0  1 -1

Matrix B:
2 0 0
0 2 0
0 0 2

output:
1 0 2
3 0 1
0 1 3*/

普通函数接收一个类的成员函数作为参数传递

NOTE : 这里是这个类已经实例化了,在类的一个成员函数a中调用普通函数,并且将这个类的成员函数b作为参数传入这普通函数中, 普通函数中接收到了这个类实例化后的所有信息

该目录下包含文件:
|--main.cpp
|--test_fun.hpp
|--test_class.hpp

example
main.cpp


#include <iostream>
#include <string>
#include <vector>

#include "test_class.hpp"

using namespace std;

//方法1 使用非静态成员函数
//方法2 使用静态成员函数
//方法3 使用bind试一试

int main(){

    vector<int> v = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

    /* method 1:将类内非静态成员函数作为普通函数的参数传入(具体体现在这个类的test_A函数实现中)   
                从头到尾巴只实例化了一个类,且完成这个类中的参数修改   */
    A my_a;
    my_a.test_A(v);  
    
    return 0; 
    /* method 1:      */

}

test_class.hpp



#ifndef TEST_CLASS_H
#define TEST_CLASS_H

#include <iostream>
#include <vector>
#include "test_fun.hpp"

using namespace std;



class A;
void test_accumulation(A *a, vector<int>(A::*f)(vector<int>&), vector<int> v);
void test_minus(A* a, vector<int> v);

class A{
public:
    vector<int> m_v;

public:
    A(){
        cout << "initialize class A" << endl;
        for (int i=0; i<10; i++)
            m_v.push_back(i);
    }


    void test_A(vector<int> v){
        std::cout << "first time to call" << endl;
        test_accumulation(this, &A::accumulation, v);//需要传入this指针
        std::cout << std::endl;
        this->m_v[9] = 100; //改变一下成员变量重复上次调用
        std::cout << "second time to call" << endl;
        test_accumulation(this, &A::accumulation, v);
    }

    vector<int> accumulation(vector<int> &v){
        if (v.size() != m_v.size())
            return vector<int>{};
        
        cout << "sum operation" << endl;
        for (int i=0; i<v.size(); ++i)  
            v[i] += m_v[i];
        
        return v;
    }   

    //以下属于本例中无效代码
    //静态成员函数没有隐含式参数this,下面是错误写法,正确写法要传入一个类,如下
    static vector<int> minuss(A *pthis, vector<int> v){
        if (v.size() != pthis->m_v.size())
            return vector<int>{};
        
        for (int i=0; i<v.size(); ++i)  
            v[i] -= pthis->m_v[i];
        
        return v;
    }



};

#endif

test_fun.hpp

#ifndef TEST_FUN_H
#define TEST_FUN_H

#include "test_class.hpp"
using namespace std;


class A;

void test_accumulation(A *a, vector<int>(A::*f)(vector<int>&), vector<int> v){

    auto result = (a->*f)(v);//调用类中非静态成员函数

    for (auto i: result)
        std::cout << i << "--";

    cout << endl;
} 


#endif

output

//method 1.
initialize class A
first time to call
sum operation
11--13--15--17--19--21--23--25--27--29--

second time to call
sum operation
11--13--15--17--19--21--23--25--27--120--


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值