나는 포인터의 벡터를 가지고있다. 모든 요소에 대해 함수를 호출하고 싶습니다. 그러나이 함수는 참조를 취합니다. 요소를 역 참조 할 수있는 간단한 방법이 있습니까?for_each와 boost :: bind를 포인터 벡터로 사용하기
예 :
MyClass::ReferenceFn(Element & e) { ... }
MyClass::PointerFn(Element * e) { ... }
MyClass::Function()
{
std::vector< Element * > elements;
// add some elements...
// This works, as the argument is a pointer type
std::for_each(elements.begin(), elements.end(),
boost::bind(&MyClass::PointerFn, boost::ref(*this), _1));
// This fails (compiler error), as the argument is a reference type
std::for_each(elements.begin(), elements.end(),
boost::bind(&MyClass::ReferenceFn, boost::ref(*this), _1));
}
내가 포인터를 취하는 더러운 래퍼를 만들 수 있습니다,하지만 난이 생각하는 더 좋은 방법이 될했다?
'boost :: ref (* this)'를 사용하는 이유가 있습니까? 난 그냥 사용 : boost :: bind (& MyClass :: ReferenceFn, this, _1) 잘 작동합니다. –