473,671 Members | 2,282 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(childr en.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 4531

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(childr en.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(childr en.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.goo glegroups.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.in digo.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.in digo.ie>, Frederick Gotham
<fg*******@SPA M.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
4638
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
11245
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
3972
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 bound inside struct or class to become global functions. It makes easier for me to use "struct.memberfunction()" instead of "globalfunction()" when I have to use dot between struct and member function rather than global function. I do not have...
110
9904
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 must be an object instead of
5
7181
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
11460
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 this a managed struct by adding either the value struct or ref struct I received a compile error stating that mixed types are not supported. I understand this, but I don't know how to create a char array in
1
2255
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 a b-spline surfcae. The problem class is the polygon class. this class read 3d files generates faces, edges and so on. here are two of the building blocks(structs) struct FACE{
9
2636
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 simple straightforward way that a simple guy from "Missourah" can understand ?
21
10570
by: Zytan | last post by:
Is it possible, as in C? I don't think it is. Just checking. Zytan
0
8474
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8392
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.