Im new to C++. 각 get-method 매핑 된 get-method 이름의 문자열을 사용하여 std :: map을 만들고 싶습니다. 이것들은 반복되고, get-method에 의해 얻어진 값을 메소드 이름과 함께 제시해야한다. A 유형의 여러 인스턴스를 반복 할 것입니다. boost/function이 A에 get-methods를 저장하는 데 매우 유용하다는 것을 알았습니다. 그러나 A는 자체 get-methods를 가진 B 유형의 인스턴스도 가지고 있습니다. B에서 get-method에 어떻게 액세스합니까? 여기에 내 현재 코드가함수 포인터가 반환 한 객체 인스턴스에 대한 함수 포인터
입니다 (행 mapping["B_Nine"] = &B::(&A::getB)::nine
잘못이지만, 내 추측 지금까지) ...
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <iostream>
#include <map>
#include <string>
#include <functional>
class B
{
public:
B();
~B();
int nine() {return 9;}
};
B::B()
{
}
B::~B()
{
}
class A
{
public:
A();
~A();
int one() {return 1;}
int two() {return 2;}
B getB() {return b;}
B b;
};
A::A()
{
b = B();
}
A::~A()
{
}
typedef std::map<std::string,boost::function<int (A*)>> str_func_map;
int main()
{
str_func_map mapping;
mapping["One"] = &A::one;
mapping["Two"] = &A::two;
mapping["B_Nine"] = &B::(&A::getB)::nine //This line is wrong - how should
//it be done correctly??
A* a = new A();
for (str_func_map::iterator i = mapping.begin(); i != mapping.end(); i++)
{
std::cout<< i->first << std::endl;
std::cout<< (i->second)(a) << std::endl;
}
system("pause");
}
그리고 당신은 어떤 오류가 발생 했습니까? BTW, C++ 11 이상에 액세스 할 수있는 경우이 많은 것들이 표준 라이브러리로 옮겨졌습니다. – Useless
박쥐 오른쪽에서, 당신은 a.getB()에 대한 호출과 같고'(& A :: getB)'를 사용하고 있지만 그렇지 않습니다. * pointer * 함수가 있습니다. 인스턴스가 아닌 경우 인스턴스를 호출 할 수 있어야합니다. – Neil
OK, 그렇게 해결할 수 없습니까? B에서 함수 포인터에 대한 포인터를 만들 수 없습니까? – Frysen