By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Why use struct instead of class while using functors?

P: n/a
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

Jun 29 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

mi*************@gmail.com wrote:
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

Jun 29 '06 #2

P: n/a
mi*************@gmail.com wrote:
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?

--
Ian Collins.
Jun 29 '06 #3

P: n/a
Hello,

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

Thanks for the help!,
Michael

Jun 29 '06 #4

P: n/a

<mi*************@gmail.com> skrev i meddelandet
news:11**********************@m73g2000cwd.googlegr oups.com...
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
Jun 29 '06 #5

P: n/a
Bo Persson wrote:
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.


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 '{'.

Jun 29 '06 #6

P: n/a

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;
};


--

Frederick Gotham
Jun 29 '06 #7

P: n/a
In message <wj*******************@news.indigo.ie>, Frederick Gotham
<fg*******@SPAM.com> writes

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.

--
Richard Herring
Jun 29 '06 #8

P: n/a
Richard Herring posted:
In message <wj*******************@news.indigo.ie>, Frederick Gotham
<fg*******@SPAM.com> writes

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.

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

--

Frederick Gotham
Jun 29 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.