func1(int a,int b)
func2(int c, int d )
func3(aa,bb,(*fun)()   )
{
   ...
   
        fun(aa,bb);
   
   ...
}在func4里调用:if i=1
  
  func3(a,b,func1);
else
  
  func3(a,b,func2);
这些函数函数在一个类里。
func3可以用两个函数实现,但用一个更简洁些,因为该函数大部分代码相同。这种想法是否合理?这些函数都在一个类里。
这样可以用函数指针实现,但是调用时却会出现类成员函数指针无法转换为普通函数指针的错误提示:
error C2440: '=' : cannot convert from 'int (__thiscall MyDoc::* )(int,int)' to 'int (__cdecl *)(int,int)'
是否可以强制转换?
请教如何解决或是否有更好思路?谢谢各位!

解决方案 »

  1.   

    声明函数的时候,直接声明函数的调用方式为_cdecl 
      

  2.   

    谢谢!
    是声明func1和func2两个函数吗?
    还是所有的?
    怎么声明函数调用方式?这个不懂,请教!
      

  3.   

    void _cdecl func1(int a,int b)
      

  4.   

    好像不行,错误:
    error C2440: '=' : cannot convert from 'int (__cdecl MyDoc::* )(int,int)' to 'int (__cdecl *)(int,int)'
      

  5.   

    MyDocn你设置的一个是成员函数 一个是外部函数,
    设法一致
      

  6.   

    谢谢指教!
    func1、func2、func3、func4都是在一个类MyDoc里声明的。
    只剩下fun函数指针,应该怎么声明?
    在请教!
      

  7.   

    void (__stdcall CMyDoc::*Func)(int a,int b);
      

  8.   

    现在在func3函数里出现以下错误:
    error C2064: term does not evaluate to a function taking 2 arguments
    似乎是fun并没有指向一个函数地址。
    不好意思,再次麻烦了!
    其中func1和func2会返回int值,不知有没有影响?在类的头文件里
    typedef int (__cdecl CMyDoc::*pFunc)(CString ,CString ,CString ,CString ,BOOL);
    int __cdecl func1(int,int);
    int __cdecl func2(int,int);在.cpp文件
     func3(int aa,int bb,pFunc fun   )
    {
       ...
          fun(aa,bb);
       ...
    }