[問題] function pointer指錯
我最近寫了這樣的程式碼,環境是VS 2008:
int cmp(int* PtrX, int* PtrY){
//// local variable ////
int result;
int X, Y;
X = *PtrX ;
Y = *PtrY ;
if(X<Y)
result = -1;
else if(X>Y)
result = 1;
else
result = 0;
return result;
}
class A{
public:
int (*compare)(int*,int*);
A( int (*compare)(int*,int*) ){
compare = compare;
};
};
int main(){
//// Creat list ////
A* ptrA = new A(cmp);
int a = 3;
int b = 2;
cout<<ptrA->compare(&a,&b)<<endl;
system("pause");
return 0;
}
這樣子寫compiler給過,且理想上,a>b
經過比較的function後,應該要回傳 +1,所以cout應該也是1才對,
不過我剛剛去跑,竟然會出現access violation,
真是令人百思不解,這是怎麼一回事?
有板友能指導教學一下嗎?
在這先謝謝大家了~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.144.18
※ 編輯: kkroy 來自: 140.113.144.18 (03/04 04:24)
推
03/04 04:31, , 1F
03/04 04:31, 1F
→
03/04 04:31, , 2F
03/04 04:31, 2F
→
03/04 04:41, , 3F
03/04 04:41, 3F
→
03/04 11:12, , 4F
03/04 11:12, 4F
※ 編輯: kkroy 來自: 140.113.144.18 (03/04 17:18)
→
03/04 17:18, , 5F
03/04 17:18, 5F