Why use struct instead of class while using functors?

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
 
P

peter koch

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.

They do not.
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?

You can. struct and class is equivalent except for accessibility. The
problem most likely is that you forgot to make your operator() public.
Thanks for explanations,
Michael

/Peter
 
I

Ian Collins

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> {
Did you make the operator() public?
 
M

michael.lesniak

Hello,

forgot to make operator() public, as both of you mentioned.

Thanks for the help!,
Michael
 
B

Bo Persson

Hello,

forgot to make operator() public, as both of you mentioned.

Which is why many prefer to use a struct in the first place. If
everything is supposed to be public, that is the easiest.


Bo Persson
 
R

Rolf Magnus

Bo said:
Which is why many prefer to use a struct in the first place. If
everything is supposed to be public, that is the easiest.

However, in classes, I always put the public members first, too, so every
class begins with something like:

class Whatever
{
public:


It seems more logical to me to put the members in order of the number of
users, and so I get public first, then protected and at the end private.

Then, when defining a struct, I also get that urge to write the 'public' on
inheritance and after the '{'.
 
F

Frederick Gotham

The following are equivalent:


struct Base : Derived
{
int i;
};

class Base : public Derived /* Note the inheritance also */
{
public:

int i;

};


As are the following:


struct Base : private Derived
{
private:

int i;

};

class Base : Derived
{
int i;
};
 
R

Richard Herring

Frederick Gotham said:
The following are equivalent:


struct Base : Derived
{
int i;
};

class Base : public Derived /* Note the inheritance also */
{
public:

int i;

};
So your derived struct/class is called Base, and the base one is called
Derived. Hmmm.
 
F

Frederick Gotham

Richard Herring posted:
So your derived struct/class is called Base, and the base one is called
Derived. Hmmm.


At it's not a logic error in the computer sense of the term ; )
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top