P
Przemyslaw Koprowski
Hi all,
I have the following problem: take a class
that is intended to interpret strings char_by_char
depending on its internal state (that changes during
the interpretation) i.e. something like a finite_state_machine
(not quite but close).
The class has a member that interprets a single char ('doit'
in the example code below) and a member to intrepret a whole
string calling the previous one for each character.
The question is: can the later member use for_each template?
If so, how to pass the former method as the one to be called
on each char?
Here is a simple example:
#include <string>
class interp {
private:
// some internal state, e.g
int x, y;
public:
// c-tor sets-up initial state e.g.
interp() : x(0), y(0) {};
// members to interpret chars/strings
void doit(char);
void doit(const std::string &s);
// ...
};
void interp::doit(char c)
{
// interpret c and do sth. depending on
// internal state, possibly change the state
// e.g.
switch( c ) {
case '+' : x += y; break;
case '-' : y += 7; break;
// ...
}
}
void interp::doit(const std::string &s)
{
// interpret the string char by char
std::for_each(
s.begin(),
s.end(),
doit // ERROR
);
}
int main()
{
interp C;
C.doit("+-0");
return 0;
}
The marked line causes an error since the name 'doit'
is ambigious (that's the first problem how to resolve
the ambiguity). Even if I change the names of the two
methods (say 'doit1', 'doit2') it doesn't help, as this
is still a member function not a "regular" one.
Notice that mem_fun adaptor doesn't help here (at least
directly) since I don't want to call a member of each
element in the collection (being char and so not having
any members). Rather I want to call a member of different
class.
Thanks in advance,
Przemek
P.S.
I can very easily do it with explicit loop (and it is a way
how I do it rigth now). The question is whether it can be
done *without* explicit loop.
I have the following problem: take a class
that is intended to interpret strings char_by_char
depending on its internal state (that changes during
the interpretation) i.e. something like a finite_state_machine
(not quite but close).
The class has a member that interprets a single char ('doit'
in the example code below) and a member to intrepret a whole
string calling the previous one for each character.
The question is: can the later member use for_each template?
If so, how to pass the former method as the one to be called
on each char?
Here is a simple example:
#include <string>
class interp {
private:
// some internal state, e.g
int x, y;
public:
// c-tor sets-up initial state e.g.
interp() : x(0), y(0) {};
// members to interpret chars/strings
void doit(char);
void doit(const std::string &s);
// ...
};
void interp::doit(char c)
{
// interpret c and do sth. depending on
// internal state, possibly change the state
// e.g.
switch( c ) {
case '+' : x += y; break;
case '-' : y += 7; break;
// ...
}
}
void interp::doit(const std::string &s)
{
// interpret the string char by char
std::for_each(
s.begin(),
s.end(),
doit // ERROR
);
}
int main()
{
interp C;
C.doit("+-0");
return 0;
}
The marked line causes an error since the name 'doit'
is ambigious (that's the first problem how to resolve
the ambiguity). Even if I change the names of the two
methods (say 'doit1', 'doit2') it doesn't help, as this
is still a member function not a "regular" one.
Notice that mem_fun adaptor doesn't help here (at least
directly) since I don't want to call a member of each
element in the collection (being char and so not having
any members). Rather I want to call a member of different
class.
Thanks in advance,
Przemek
P.S.
I can very easily do it with explicit loop (and it is a way
how I do it rigth now). The question is whether it can be
done *without* explicit loop.