【问题标题】:C++ - function pointers and classC++ - 函数指针和类
【发布时间】:2015-12-25 15:08:36
【问题描述】:

当我试图编译这段代码以使用 C++ 中的类来实现函数指针的概念时:

#include <iostream>

using namespace std;

class sorting
{
public:
void bubble_sort(int *arr, int size, bool (*compare)(int,int))
{
    int i,j,temp = 0;

    for(i = 0; i < size - 1; i++)
    {
        for(j = i+1; j < size; j++)
        {
            if(compare(arr[i],arr[j]))
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j]  = temp;
            }
        }
    }
}

bool ascending(int x,int y)
{
    return x > y;
}

bool descending(int x,int y)
{
    return x < y;
}

void display(int *arr,int size)
{
    for(int index = 0; index < size; index++)
    {
        cout<<"arr["<<index<<"]:"<<arr[index]<<endl;
    }
}
};

int main()
{
    int arr[10] = {99,77,22,33,88,55,44,66,11,100};

    sorting s;
    cout<<"Ascending order"<<endl;
    s.bubble_sort(arr,10,&sorting::ascending);
    s.display(arr,10);

    cout<<"Descending order"<<endl;
    s.bubble_sort(arr,10,&sorting::descending);
    s.display(arr,10);

    return 0;
}

我在这些行中有错误:

s.bubble_sort(arr,10,&sorting::ascending);
s.bubble_sort(arr,10,&sorting::descending);

错误是:

error C2664: 'sorting::bubble_sort' : cannot convert parameter 3 from 'bool (__thiscall sorting::* )(int,int)' to 'bool (__cdecl *)(int,int)'

对于这两行。 有人可以帮我消除这些错误吗?

【问题讨论】:

  • 最简单的解决方案?制作比较函数static。然后跟我重复一遍:指向函数的指针与指向成员函数的指针不同
  • 最简单的解决方法是将ascendingdescending 标记为static

标签: c++ function class pointers


【解决方案1】:

ascendingdescending 是成员函数,因此它们只能在 sorting 类成员上调用(实际上有三个参数,而不是两个)。

使它们成为static 函数,或者更好的是,将sortingclass 更改为namespace:它没有理由成为一个类。

【讨论】:

    【解决方案2】:

    最简单的方法是在类定义内部或外部定义ascendingdescending 静态函数,或者将函数声明/定义移到类范围之外:

    static bool ascending(int x,int y)
    {
        return x > y;
    }
    
    static bool descending(int x,int y)
    {
        return x < y;
    }
    

    编译器将类中定义的函数视为使用__thiscall调用约定的成员函数,而普通C++函数指针默认使用__cdecl调用约定(可以手动修改)。将定义移到类之外会自动为函数提供__cdecl 修饰符。将函数标记为 static 也会为它们提供 __cdecl 修饰符。

    【讨论】:

      【解决方案3】:

      sorting::ascendingsorting::descending 应该声明为static,或者根本不作为成员,因为它们不对*this 实例进行操作。这就是您知道函数应该是static(或非成员)的方式。

      如果没有将它们声明为static,则成员函数指针的语法会有所不同,并且您还需要一个虚拟实例来进行调用。

      【讨论】:

        【解决方案4】:

        C++ 中的函数指针略有不同。与 C 不同,成员函数并不总是“全局”可访问的,除非将其设为 static。当创建static 时,对于该类的任意数量的实例,该函数只有一个地址位置。您使用此方法的代码将是:

        #include<iostream>
        
        using namespace std;
        
        class sorting
        {
        public:
        void bubble_sort(int *arr, int size, bool (*compare)(int,int))
        {
            int i,j,temp = 0;
        
            for(i = 0; i < size - 1; i++)
            {
                for(j = i+1; j < size; j++)
                {
                    if(compare(arr[i],arr[j]))
                    {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j]  = temp;
                    }
                }
            }
        }
        
        static bool ascending(int x,int y)
        {
            return x > y;
        }
        
        static bool descending(int x,int y)
        {
            return x < y;
        }
        
        void display(int *arr,int size)
        {
            for(int index = 0; index < size; index++)
            {
                cout<<"arr["<<index<<"]:"<<arr[index]<<endl;
            }
        }
        };
        
        int main()
        {
            int arr[10] = {99,77,22,33,88,55,44,66,11,100};
        
            sorting s;
            cout<<"Ascending order"<<endl;
            s.bubble_sort(arr,10,&sorting::ascending);
            s.display(arr,10);
        
            cout<<"Descending order"<<endl;
            s.bubble_sort(arr,10,&sorting::descending);
            s.display(arr,10);
        
            return 0;
        }
        

        另一种方法是使用.*-&gt;* 关键字。它们与类的实例一起使用,以调用它们的(非静态)成员函数之一。如果调用发生在另一个成员函数中,您可以使用 this 指针。阅读here 了解更多详情。 Ref。您的代码将是:

        #include<iostream>
        
        using namespace std;
        
        class sorting
        {
        public:
        void bubble_sort(int *arr, int size, bool (sorting::*compare)(int,int))
        {
            int i,j,temp = 0;
        
            for(i = 0; i < size - 1; i++)
            {
                for(j = i+1; j < size; j++)
                {
                    if((this->*compare)(arr[i],arr[j]))
                    {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j]  = temp;
                    }
                }
            }
        }
        
        bool ascending(int x,int y)
        {
            return x > y;
        }
        
        bool descending(int x,int y)
        {
            return x < y;
        }
        
        void display(int *arr,int size)
        {
            for(int index = 0; index < size; index++)
            {
                cout<<"arr["<<index<<"]:"<<arr[index]<<endl;
            }
        }
        };
        
        int main()
        {
            int arr[10] = {99,77,22,33,88,55,44,66,11,100};
        
            sorting s;
            cout<<"Ascending order"<<endl;
            s.bubble_sort(arr,10,&sorting::ascending);
            s.display(arr,10);
        
            cout<<"Descending order"<<endl;
            s.bubble_sort(arr,10,&sorting::descending);
            s.display(arr,10);
        
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-31
          • 1970-01-01
          • 2018-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多