2

I'm trying to access a member function of a nested class in a find_if expression.

My code below causes a compile error in the bind expression - (‘COuter::innerClass’ is not a class or namespace).

Could you help me with the correct bind expression?

vector<COuter> vec; vec.push_back(COuter()); vector<COuter>::const_iterator it = find_if(vec.begin(), vec.end(), bind(&COuter::innerClass::GetTemp, _1) == 42); 

My example classes:


class CInner { public: CInner() : _temp(42) {}; int GetTemp() const { return _temp; } private: int _temp; }; class COuter { public: CInner innerClass; }; 

1 Answer 1

3

Correct expression is bind(&CInner::GetTemp, bind(&COuter::innerClass, _1)).

Sign up to request clarification or add additional context in comments.

3 Comments

What is this extra bind doing there? You should just pass in a reference to the member.
@CashCow: you need an extra bind to traverse through the extra object innerClass. It works as it should with gcc-4.4.
@CashCow, the desired ordinary-C++ expression is this: vec[i].innerClass.GetTemp() == 42. The _1 variable will refer to vec[i], so the inner bind expression gives us a reference to vec[i].innerClass. The outer bind gets us its GetTemp member.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.