指向类的成员函数的指针????请帮忙看看代码
/****************************************************************/
//定义了一个结构
/****************************************************************/
struct MyStruct
{
AnsiString str1;
AnsiString str2;
......
};
/****************************************************************/
//做一个列表类,存储上面定义的结构
/****************************************************************/
enum enmSortType { stStr1Asc, stStr1Desc, stStr2Asc, stStr2Desc };
class MyList
{
private:
int SortType;
TList *pList;
......
public:
MyList()
{
SortType = stStr1Desc;
pList = new TList;
......
}
~MyList()
{
.......
delete pList;
}
int __fastcall Add(MyStruct* item);
......
int __fastcall Comparer(MyStruct* item1, MyStruct* item2);
void __fastcall Sort();
};
/****************************************************************/
int __fastcall MyList::Add(MyStruct* item)
{
if(item)
return pList->Add(item);
return -1;
}
/****************************************************************/
......
//***************************************************************/
// 下面的部分开始有问题
// Comparer方法不能用
/****************************************************************/
int __fastcall MyList::Comparer(MyStruct* item1, MyStruct* item2)
{
switch(SortType)
{
default:
case stStr1Asc:
return StrComp( item1->str1.c_str(), item2->str1.c_str() );
case stStr1Desc:
return StrComp( item2->str1.c_str(), item1->str1.c_str() );
case stStr2Asc:
return StrComp( item1->str2.c_str(), item2->str2.c_str() );
case stStr2Desc:
return StrComp( item2->str2.c_str(), item1->str2.c_str() );
}
}
/****************************************************************/
// 改成下面4个函数
/****************************************************************/
int __fastcall CompareStr1Asc( MyStruct* Item1, MyStruct* Item2 )
{
return StrComp( Item1->str1.c_str(), Item2->str1.c_str() );
}
/****************************************************************/
int __fastcall CompareStr1Desc( MyStruct* Item1, MyStruct* Item2 )
{
return -StrComp( Item1->str1.c_str(), Item2->str1.c_str() );
}
/****************************************************************/
int __fastcall CompareStr2Asc( MyStruct* Item1, MyStruct* Item2 )
{
return StrComp( Item1->str2.c_str(), Item2->str2.c_str() );
}
/****************************************************************/
int __fastcall CompareStr2Desc( MyStruct* Item1, MyStruct* Item2 )
{
return -StrComp( Item1->str2.c_str(), Item2->str2.c_str() );
}
/****************************************************************/
void __fastcall MyList::Sort()
{
/////////////////////////////////////////////////////////////////
// 下面注释掉的一句编译通不过,不知道类的成员函数为什么
// 不可以象一般非类的成员函数一样进行指针操作。错误提示见帖子尾。
// pList->Sort( (TListSortCompare)Comparer );
// 按照提示改成
// pList->Sort( (TListSortCompare)&MyList::Comparer );
// 还是不行,怪呀。
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
// 只好用下面的方法,通过倒是通过了,
// 但代价是写了上面的4个非MyList类的函数
/////////////////////////////////////////////////////////////////
TListSortCompare CompareFunc;
switch( SortType )
{
default:
case stStr1Asc:
CompareFunc = (TListSortCompare) CompareStr1Asc;
break;
case stStr1Desc:
CompareFunc = (TListSortCompare) CompareStr1Desc;
break;
case stStr2Asc:
CompareFunc = (TListSortCompare) CompareStr2Asc;
break;
case stStr2Desc:
CompareFunc = (TListSortCompare) CompareStr2Desc;
break;
}
pList->Sort( CompareFunc );
}
//////////////////////////////////////////////////////////////////////////////////////
错误提示的联机帮助(曾经以为自己真的看懂了,呵呵):
E2235 Member function must be called or its address taken
A reference to a member function must be called, or its address must be taken with & operator.
In this case, a member function has been used in an illegal context.
For example:
class A
{
typedef int (A::* infptr)(void);
public:
A();
int myex(void);
int three;
} a;
A::A()
{
infptr one = myex; //illegal - call myex or take address?
infptr two = &A::myex; //correct
three = (a.*one)() + (a.*two)();
}
////////////////////////////////////////////////////////////////////////////////////