473,511 Members | 15,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why use struct instead of class while using functors?

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
8 4516

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
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
Hello,

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

Thanks for the help!,
Michael

Jun 29 '06 #4

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4617
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
2
11221
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
1
3950
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
110
9812
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
5
7178
by: Chua Wen Ching | last post by:
Hi there, I am very curious on this code: // declared as structure public struct Overlapped { public IntPtr intrnal; public IntPtr internalHigh;
3
11440
by: GrkEngineer | last post by:
I recently had to use someone's struct from a native app to receive data over Udp. The struct has a array member which looked like this: struct sensorHdr{ char sName; }; When I tried to make...
1
2235
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
9
2623
by: Bit Byte | last post by:
Can't seem to get my head around the point of a trait class - no matter how many times I read up on it - why not simply use functors or function pointers ? Anyone care to explain this in a...
21
10537
by: Zytan | last post by:
Is it possible, as in C? I don't think it is. Just checking. Zytan
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7144
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.