C++ 如何将类中的公共函数作为另一个函数中的参数传递。

C++ 如何将类中的公共函数作为另一个函数中的参数传递。,c++,function,oop,pointers,parameters,C++,Function,Oop,Pointers,Parameters,我试图将类内的“get”函数作为参数传递给类外的“display”函数。 我在网络上到处寻找这样做的方法,但似乎没有一个明确的答案。 AAMOI-你可以不用指针来做这件事吗 #include <iostream> using namespace std; class student{ public: void setVAr(int x, int y, int z){ phone=x; house=y; rollno=z;

我试图将类内的“get”函数作为参数传递给类外的“display”函数。 我在网络上到处寻找这样做的方法,但似乎没有一个明确的答案。 AAMOI-你可以不用指针来做这件事吗

#include <iostream>
using namespace std;
class student{
public:
    void setVAr(int x, int y, int z){
        phone=x;
        house=y;
        rollno=z;
    };
    int getPhone(){
         return phone;
    };
    int getHouse(){
        return house;
    };
    int getRollno(){
        return rollno;
    };
private:
    int phone;
    int house;
    int rollno;
};
void display(student anyStu, int * funcA, int * funcB, int * funcC);
int main(){
    int x=0,y=0,z=0;
    student jack;
    cout<<"Please enter in your phone no:"<<endl;
    cin>>x;
    cout<<"Please enter in your house no.: "<<endl;
    cin>>y;
    cout<<"Enter your rollno: "<<endl;
    cin>>z;
    jack.setVAr(x,y,z);

    cout<<"Your Phone no is: "<<jack.getPhone()<<endl;
    cout<<"Your house no: "<<jack.getHouse()<<endl;
    cout<<"Your rollno no: "<<jack.getRollno()<<endl;
    display(jack,&jack.getPhone(),&jack.getHouse(),&jack.getRollno());
};
void display(student anyStu,int * funcA, int * funcB, int * funcC){
    cout<<"Your Phone no is: "<<anyStu.getPhone()<<endl;
    cout<<"Your house no: "<<anyStu.getHouse()<<endl;
    cout<<"Your rollno no: "<<anyStu.getRollno()<<endl;
};
#包括
使用名称空间std;
班级学生{
公众:
void setVAr(整数x,整数y,整数z){
电话=x;
豪斯=y;
rollno=z;
};
int getPhone(){
回电话;
};
int getHouse(){
归还房屋;
};
int getRollno(){
返回rollno;
};
私人:
国际电话;
内特之家;
int-rollno;
};
无效显示(学生anyStu,int*funcA,int*funcB,int*funcC);
int main(){
int x=0,y=0,z=0;
学生杰克;

cout首先,通过引用传递
anyStu
,比如
void display(const student&anyStu,…)
这样您就不会复制对象(并使其
const
作为一种良好的度量,以表明您没有修改它)。还可以使get函数像这样const
int getHouse()const{
;其次,如果您已经有了引用,为什么不直接使用get函数(例如,
anyStu.getPhone()
)而不是尝试传递它们(因为它们是实例的一部分,所以您不能这样做)?如果不适合,接口类可能是合适的

void display(const student &anyStu){
    cout<<"Your Phone no is: "<<anyStu.getPhone()<<endl;
    cout<<"Your house no: "<<anyStu.getHouse()<<endl;
    cout<<"Your rollno no: "<<anyStu.getRollno()<<endl;
};

从您的代码示例中不完全清楚您为什么需要它,但我认为您需要的是一个指向成员函数的指针。例如,这将创建一个指向成员函数的指针
getPhone
称为
funcA

int(student::*funcA)() = &student::getPhone;
或者在C++11中,只需:

auto funcA = &student::getPhone;
然后您可以通过执行以下操作来调用它:

(jack.*funcA)()
我更喜欢使用
typedef
s或C++11
使用
别名作为指向成员函数的指针,因为语法可能会有点混乱。然后,您的示例合并了Mark的一些建议,如下所示:

#include <iostream>

class student {
public:
    void setVAr(int x, int y, int z){
        phone=x;
        house=y;
        rollno=z;
    }
    int getPhone() const { return phone; }
    int getHouse() const { return house; }
    int getRollno() const { return rollno; }
private:
    int phone;
    int house;
    int rollno;
};

using Func = int (student::*)() const;  // C++11
//typedef int(student::Func*)() const;  // C++98

void display(const student& anyStu, Func funcA, Func funcB, Func funcC){
    std::cout<<"A: "<<(anyStu.*funcA)()<<"\n";
    std::cout<<"B: "<<(anyStu.*funcB)()<<"\n";
    std::cout<<"C: "<<(anyStu.*funcC)()<<"\n";
}

int main(){
    student jack;
    jack.setVAr(911,42,1);

    display(jack, &student::getPhone, &student::getHouse, &student::getRollno);
}

.

funcA/B/C是保存get.Phone/.House/.Rollno参数的参数。函数非常简单,但很难将函数传递到函数中,关于这方面的教程很少。我认为这并不能真正回答问题。你是对的,它没有,但感觉更像是一个教育/指导型的答案。。。
#include <iostream>

class student {
public:
    void setVAr(int x, int y, int z){
        phone=x;
        house=y;
        rollno=z;
    }
    int getPhone() const { return phone; }
    int getHouse() const { return house; }
    int getRollno() const { return rollno; }
private:
    int phone;
    int house;
    int rollno;
};

using Func = int (student::*)() const;  // C++11
//typedef int(student::Func*)() const;  // C++98

void display(const student& anyStu, Func funcA, Func funcB, Func funcC){
    std::cout<<"A: "<<(anyStu.*funcA)()<<"\n";
    std::cout<<"B: "<<(anyStu.*funcB)()<<"\n";
    std::cout<<"C: "<<(anyStu.*funcC)()<<"\n";
}

int main(){
    student jack;
    jack.setVAr(911,42,1);

    display(jack, &student::getPhone, &student::getHouse, &student::getRollno);
}
#include <functional>

using Func = std::function<int(const student&)>;

void display(const student& anyStu, Func funcA, Func funcB, Func funcC){
    std::cout<<"A: "<<funcA(anyStu)<<"\n";
    std::cout<<"B: "<<funcB(anyStu)<<"\n";
    std::cout<<"C: "<<funcC(anyStu)<<"\n";
}

int main(){
    student jack;
    jack.setVAr(911,42,1);

    auto getPhone = [](const student& s){ return s.getPhone(); };
    auto getHouse = [](const student& s){ return s.getHouse(); };
    auto getRollno = [](const student& s){ return s.getRollno(); };

    display(jack, getPhone, getHouse, getRollno);
}