nberic1 2021-10-12 10:10 采纳率: 0%
浏览 21

这段程序,在DEV-C++中也提示出错


#include <iostream>
using namespace std;
class Test
{
    int x,y;
    public:
        Test(int i,int j=0)
        {x=i;y=j;}
        int get(int i ,int j)
        {return i+j;}
        
};

int main()
{
    Test t1(2),t2(4,6);
    int (Test::*p)(int ,int=10);  //看不懂
    p=Test::get;
    cout<<(t1.*p)(5)<<endl;  //看不懂
    Test *p1=&t2;
    cout<<(p1->*p)(7,20)<<endl; //看不懂
    return 0;
    
}

}

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-10-12 10:12
    关注

    看不懂的是函数指针申明和调用
    修改如下:

    int main()
    {
        Test t1(2),t2(4,6);
        typedef int (Test::*p)(int, int);
        p fun =&Test::get;
        cout<<(t1.*fun)(5,1)<<endl;  //看不懂
        Test *p1=&t2;
        cout<<(p1->*fun)(7,20)<<endl; //看不懂
        return 0;
        
    }
    
    
    评论

报告相同问题?

问题事件

  • 修改了问题 10月12日
  • 创建了问题 10月12日