M
michael.lesniak
Hello,
I'm learning C++ for a couple of days and play a bit with the
algorithms provided in the STL. One thing I don't understand is the
fact that classes inherited of functors have to be defined using
structs.
The code
template<class type> struct Print : public unary_function<type, void> {
void operator()(type& x) {
cout << x << endl;
}
};
in conjunction with
for_each(children.begin(), children.end(), Print<Node<type>*>());
does only work with struct but not with class, i.e.
template<class type> class Print : public unary_function<type, void> {
leads to a compiler error. I thought that classes and structs are more
or less equal, esp. since unary_function is a class itself, so why
can't I just inherit of it?
Thanks for explanations,
Michael
I'm learning C++ for a couple of days and play a bit with the
algorithms provided in the STL. One thing I don't understand is the
fact that classes inherited of functors have to be defined using
structs.
The code
template<class type> struct Print : public unary_function<type, void> {
void operator()(type& x) {
cout << x << endl;
}
};
in conjunction with
for_each(children.begin(), children.end(), Print<Node<type>*>());
does only work with struct but not with class, i.e.
template<class type> class Print : public unary_function<type, void> {
leads to a compiler error. I thought that classes and structs are more
or less equal, esp. since unary_function is a class itself, so why
can't I just inherit of it?
Thanks for explanations,
Michael