[英]C++ - function pointers and class
當我試圖編譯這段代碼以使用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)'
兩條線。 有人可以幫助我消除這些錯誤嗎?
ascending
和descending
都是成員函數,因此它們只能在sorting
類成員上調用(實際上有三個參數,而不是兩個)。
讓他們static
功能,或者,甚至更好,更改sorting
從class
到namespace
:沒有理由為它是一類。
sorting::ascending
和sorting::descending
應該聲明為static
,或者根本不作為成員聲明,因為它們不在*this
實例上運行。 這就是你知道一個函數應該是static
(或非成員的)。
如果沒有將它們聲明為static
,則成員函數指針的語法是不同的,並且您還需要一個虛擬實例來進行調用。
最簡單的方法是在類定義的內部或外部生成ascending
和descending
靜態函數的定義,或者通過在類的范圍之外移動函數聲明/定義:
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
修飾符。
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;
}
另一種方法是使用.*
或->*
關鍵字。 它們與類的實例一起使用,以便調用它們的(非靜態)成員函數之一。 如果調用發生在另一個成員函數中,您可以使用this-pointer。 請閱讀此處了解更多詳情。 參考 那么您的代碼將是:
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.