指向类的成员函数的指针????请帮忙看看代码

Winsky 2001-11-24 08:49:14
/****************************************************************/
//定义了一个结构
/****************************************************************/
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)();
}
////////////////////////////////////////////////////////////////////////////////////
...全文
99 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Chxis 2001-11-25
  • 打赏
  • 举报
回复

gz
comanche 2001-11-24
  • 打赏
  • 举报
回复
看了半天,发现没有 TListSortCompare 的 typedef 出现
可能原因就在这. 一般来说 member function 是不能被一个指针指向的, 至少 ANSI C++ 没有这样的东东。但BCB 有新引入一个关键字 __closure, 用于指向 member function。

typedef void __fastcall (__closure *TNotifyEvent)(System::TObject* Sender);
从这 TNotifyEvent 看来你的TListSortCompare 要定义成
int __fastcall ( __closure *TListSortCompare)(MyStruct* item1, MyStruct* item2);
应该会正常

ggyy 2001-11-24
  • 打赏
  • 举报
回复
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() );
}
}
NowCan 2001-11-24
  • 打赏
  • 举报
回复
是不是要用static函数啊?
hugsnow 2001-11-24
  • 打赏
  • 举报
回复
看我主页上的文章,可能会对你有所帮助
http://bcbtop.myrice.com/use/37.htm

13,873

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧