loop -> for_each

I

ibe

Hi,
i can't get the clue:
I hav a loop in my code like this:

---snip---

class Foo {
accept(Visitor& v) {
v.visit(*this);
}
};

class Visitor {
visit (Foo& f) {
...
}
}

....in main...

Visitor v(arugments, ...);
for (std::deque<Foo*>::iterator iter = list_.begin();
iter != m_list.end();
++iter) {
(*iter)->accept(v);
}

---snip---

Now i would like to do this with for_each. I think i need std::mem_fun (or
bind1st ?!?).
Can anyone help me?

htx
ibe
 
T

Thomas Jakob

ibe said:
Hi,
i can't get the clue:
I hav a loop in my code like this:

---snip---

class Foo {
accept(Visitor& v) {
v.visit(*this);
}
};

class Visitor {
visit (Foo& f) {
...
}
}

I changed this in my example to:

struct Foo;

struct Visitor {
void visit (Foo& f) {
}
};

struct Foo {
void accept(Visitor* v) {
v->visit(*this);
}
};

Using a reference in Foo::accept will cause a "reference to reference",
if u want to use a reference boost::ref might be a solution.

....in main...

Visitor v(arugments, ...);
for (std::deque<Foo*>::iterator iter = list_.begin();
iter != m_list.end();
++iter) {
(*iter)->accept(v);
}

---snip---

Now i would like to do this with for_each. I think i need std::mem_fun (or
bind1st ?!?).
Can anyone help me?

htx
ibe

Try

std::for_each(list.begin(), list.end(),
std::bind2nd(std::mem_fun(&Foo::accept), &v));

But i would always prefer boost::bind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top