在另一个类的函数中使用一个类的函数指针 [英] Using a function pointer from one class in a function of another class

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

问题描述

我看过以下线程,但它们似乎无助于解决问题:

I have looked at the following threads, but they do not seem to help resolve the issue:

指向成员函数的函数指针-该线程无济于事,因为访问是在主班级而不是在其他班级完成

Function pointer to member function - this thread does not help because the access is made within main and not another class

一个类应调用方法使用函数指针的另一个类的说明-我不清楚如何使用<functional>标头,因此无法遵循公认的答案.

one class should invoke method of another class using a function pointer - I could not follow the accepted answer as it is not clear to me how to use <functional> header.

我有一个 U 安全的 F 连接类,UF,在其中我声明并定义了程序的所有常用实用程序函数.这些功能之一是

I have a Useful Function class, UF, in which I declare and define all common utility functions for my program. One of these functions is

int UF::compar_dbl_asc(const void *a, const void *b)//comparator for sorting 
{                                                   //array of double in ascending order
    int aa = *((int *)a), bb = *((int *)b);
    if (base_arr_dbl[aa] < base_arr_dbl[bb])
        return -1;
    if (base_arr_dbl[aa] == base_arr_dbl[bb])
        return 0;
    if (base_arr_dbl[aa] > base_arr_dbl[bb])
        return 1;
}

这是我打算在qsort()中使用的比较器函数,用于按升序对double进行排序.

This is a comparator function that I intend to use within qsort() for sorting doubles in ascending order.

UF类定义中,我也有double* base_arr_dbl;作为声明.这是qsort将使用的值(双精度)数组.

Within UF class definition, I also have double* base_arr_dbl; as a declaration. This is the array of values (doubles) that qsort will use.

现在,在另一个名为SEP的类中,我有一个函数fn1,其中我想让base_arr_dbl指向局部(到fn1)双精度数组,并且我想使用UF中的比较器函数调用qsort.具体来说,尽管可能不需要这样做,但我不对实际值的数组进行排序,而是对索引sortedindices[]的数组进行排序,以使sortedindices[0]将保存base_arr_dbl[]数组中最小条目的索引.也就是说,排序顺序为base_arr_dbl[sortedindices[0]], base_arr_dbl[sortedindices[1]],等等.

Now, in a different class of mine named SEP, I have a function, fn1 wherein I would like to have base_arr_dbl point to a local (to fn1) double array and I would like to invoke qsort using the comparator function in UF. To be very specific although this is possibly not needed, I do not sort the actual values' array, but will sort an array of indices sortedindices[], such that sortedindices[0] will hold the index of the smallest entry in base_arr_dbl[] array. That is, the sorted order is base_arr_dbl[sortedindices[0]], base_arr_dbl[sortedindices[1]], etc.

所以,我这样做:

void SEP::fn1(UF *uf) {

    double ratio[100];
    //populate ratio's entries
    //Now, sort ratio's entries.
    uf->set_base_arr_dbl(ratio); //This function is defined as
   //void UF::set_base_arr_dbl(double *ptr) { base_arr_dbl = ptr; }
    qsort(sortedindices, 100, sizeof(double), uf->compar_dbl_asc);

}

但是,qsort(sortedindices, 100, sizeof(double), uf->compar_dbl_asc);行会引发以下编译时错误:

However, the line qsort(sortedindices, 100, sizeof(double), uf->compar_dbl_asc); throws the following compile time error:

error C3867: 'USEFUL_FUNCTIONS::compar_dbl_asc': non-standard syntax; use '&' to create a pointer to member

我尝试使用&uf->compar_dbl_asc,但出现错误:

I tried having &uf->compar_dbl_asc but that gives the error:

error C2276: '&': illegal operation on bound member function expression

任何帮助解决此问题的方法都将受到赞赏.

Any way to help resolve this is appreciated.

推荐答案

正如编译器在错误消息中明确指出的那样,uf->compar_dbl_asc&(uf->compar_dbl_asc)都不适合用作qsort的参数.

As the compiler clearly tells in the error messages, neither uf->compar_dbl_asc nor &(uf->compar_dbl_asc) is appropriate for use as an argument to qsort.

您可以使用以下方法之一将compar_dbl_asc用作qsort的参数.

You can use compar_dbl_asc as an argument to qsort using one of the following approaches.

  1. 使compar_dbl_asc是该类的static成员函数.
  2. 更好的是,为您的应用创建namespace并在namespace中定义compar_dbl_asc.除非由于某些其他原因它必须是类,否则UF可能是namespace.
  1. Make compar_dbl_asc a static member function of the class.
  2. Better yet, create a namespace for your app and define compar_dbl_asc in the namespace. UF could be that namespace unless it must be a class for some other reasons.

另一种选择是放弃使用qsort来代替std::sort.后者为您提供更多选择.使用std::sort时可以使用函子或lambda函数,这将允许您使用UF::compar_dbl_asc.

Another choice would be forego use of qsort in favor of std::sort. The latter gives you more options. You can use a functor or a lambda function when you use std::sort, which will allow you to use UF::compar_dbl_asc.

std::sort(sortedindices, sortedindices+100,
          [=uf](int a, int b) { return uf->compar_dbl_asc(a, b); });

要注意的一件事是,如果选择最后一种方法,则可以将UF::compar_dbl_asc的签名更改为更加用户友好的版本.

One thing to note is that if you choose the last approach, the signature of UF::compar_dbl_asc can be changed to a more user friendly variation.

bool UF::compar_dbl_asc(int a, int b)
   return (base_arr_dbl[a] < base_arr_dbl[b]);
}

这篇关于在另一个类的函数中使用一个类的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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