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 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 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.
Hello,
forgot to make operator() public, as both of you mentioned.
Thanks for the help!,
Michael
<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
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 '{'.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
{
|
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...
|
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...
|
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;
| |
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...
|
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...
|
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...
|
by: Zytan |
last post by:
Is it possible, as in C? I don't think it is. Just checking.
Zytan
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |