473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Explicit function vs. overloaded operator?

BCC
In looking through some code I have inherited, I notice a lot of places
where the programmer used operator() as a function call:
void operator() (int x, int y);

Rather than an explicit function name:
void MyFunction(int x, int y);

Then when he instantiates a class he calls it:
MyClass myclass;
myclass (x, y);

vs.

MyClass myclass;
myclass.MyFunction(x, y);
Is there any advantage to this or is it just a style?

Thanks,
B
Jul 22 '05 #1
5 1940
BCC wrote:
In looking through some code I have inherited, I notice a lot of places
where the programmer used operator() as a function call:
void operator() (int x, int y);

Rather than an explicit function name:
void MyFunction(int x, int y);

Then when he instantiates a class he calls it:
MyClass myclass;
myclass (x, y);

vs.

MyClass myclass;
myclass.MyFunction(x, y);
Is there any advantage to this or is it just a style?


The advantage is that the user of the functor (object with overloaded
operator()) doesn't need to know whether an object of some class is
being used, instead of an ordinary function. Functors are especially
useful for encapsulating policies in generic code.
Jul 22 '05 #2
BCC wrote:
In looking through some code I have inherited, I notice a lot of places
where the programmer used operator() as a function call:
void operator() (int x, int y);

Rather than an explicit function name:
void MyFunction(int x, int y);

Then when he instantiates a class he calls it:
MyClass myclass;
myclass (x, y);

vs.

MyClass myclass;
myclass.MyFunction(x, y);
Is there any advantage to this or is it just a style?


If your object/type is used as an argument for a template that
expects a functor (a function or a type with operator() defined)
then you need operator().

Otherwise, no.

In general if you think you might need both, just implement it in
the function and then define the operator() so that it simply calls
the member function.

Victor
Jul 22 '05 #3
On Wed, 26 May 2004 18:53:48 -0400 in comp.lang.c++, Jeff Schwab
<je******@comcast.net> wrote,

The advantage is that the user of the functor (object with overloaded
operator()) doesn't need to know whether an object of some class is
being used, instead of an ordinary function. Functors are especially
useful for encapsulating policies in generic code.


Except that the user actually does depend on the type of function
or functor class being used. Only if the caller is a template
instantiated on that type do you get anywhere close to not needing to
know, and that only when the type is in fact known by the compiler.

Jul 22 '05 #4
"BCC" <br***@akanta.com> wrote in message news:t59tc.73289
In looking through some code I have inherited, I notice a lot of places
where the programmer used operator() as a function call:
void operator() (int x, int y);

Rather than an explicit function name:
void MyFunction(int x, int y);
Does he pass MyFunction objects to a template? Something like this:

template <class Iter1, class Iter2, class Action>
void for_each(Iter1 begin1, const Iter1 end1, Iter2 begin1, Action action) {
for ( ; begin1!=end1; ++begin1, ++begin2) action(*begin1, *begin2);
}

If yes, then it makes sense to write an operator(), as the template for_each
requires it. But then again we could have written for_each to call
action.MyFunction(*begin1, *begin2).

The technical advantage of operator() is that Action can be either a class
object with an operator() or a simple function. If a function, then the
type of Action is something like void (*)(int, int).

The conceptual advantage is that if class MyClass does just one thing, then
operator() reflects this one thing, and thus the essence of the class. But
I think this is a matter of style. Having an explicit function name might
make the code easier to read. For example, style.Combine(1, 2) is usually
easier to read and understand in code reviews than style(1, 2).
Then when he instantiates a class he calls it:
MyClass myclass;
myclass (x, y);
Please note you can say MyClass()(x, y), though not sure if this notation is
in popular usage.
vs.

MyClass myclass;
myclass.MyFunction(x, y);
Is there any advantage to this or is it just a style?


My guess it's a matter of style.
Jul 22 '05 #5
Siemel Naran wrote:
"BCC" <br***@akanta.com> wrote in message news:t59tc.73289

In looking through some code I have inherited, I notice a lot of places
where the programmer used operator() as a function call:
void operator() (int x, int y);

Rather than an explicit function name:
void MyFunction(int x, int y);

Does he pass MyFunction objects to a template? Something like this:

template <class Iter1, class Iter2, class Action>
void for_each(Iter1 begin1, const Iter1 end1, Iter2 begin1, Action action) {
for ( ; begin1!=end1; ++begin1, ++begin2) action(*begin1, *begin2);
}

If yes, then it makes sense to write an operator(), as the template for_each
requires it. But then again we could have written for_each to call
action.MyFunction(*begin1, *begin2).

The technical advantage of operator() is that Action can be either a class
object with an operator() or a simple function. If a function, then the
type of Action is something like void (*)(int, int).

The conceptual advantage is that if class MyClass does just one thing, then
operator() reflects this one thing, and thus the essence of the class. But
I think this is a matter of style. Having an explicit function name might
make the code easier to read. For example, style.Combine(1, 2) is usually
easier to read and understand in code reviews than style(1, 2).


If the whole purpose of the "style" object is to combine things, perhaps
the name of the object should be a verb, though the object's class name
still should be a noun:

Combiner combine;
combine( 1, 2 );

I find myself using the style quite a bit, but only in programs I plan
to enhance quite a bit, particularly those that need to do a lot of
different things for a lot of different people. The advantage is that
it pretty much forces me to create a new type for each new concept,
rather than just adding a method to an existing object that happens to
be in the right places at the right times.

Then when he instantiates a class he calls it:
MyClass myclass;
myclass (x, y);

Please note you can say MyClass()(x, y), though not sure if this notation is
in popular usage.

I've used it, but I usually feel guilty about it and end up elongating
it. :) There are a few classes for which I have actually found this
style preferable, and for those, I document the style explicitly near
the class definition. For example, rather than having a global Log
object, I create them wherever I need them. In many places, e.g. catch
blocks, I only need to write one thing to the Log before the block ends.
In the Log case, I've overloaded operator<< instead of operator(), so
the code looks like this:

Log( ) << "message";

It looks less weird when a different constructor is used:

catch( ... )
{
Log( Log::high_priority ) << "An unknown exception occurred.";
}

vs.

MyClass myclass;
myclass.MyFunction(x, y);
Is there any advantage to this or is it just a style?

My guess it's a matter of style.


Mine too. :)
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
11
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
3
by: glen stark | last post by:
Hi all. I'm working with an array of member function pointers (they are all get function of the class Bead). The typedef is: typedef _real (Bead::*_beadGfp)(void); I have a class System...
4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
2
by: RR | last post by:
I'm sure this has been answered before but two hours of searching and reading hasn't answered this question for me. Here's a test program: ******************************** class base {...
10
by: sjbrown8 | last post by:
I have the piece of code below, and when i try compiling with the line g++ 753075304.cpp I get the following error message: 753075304.cpp: In function 'int main()': 753075304.cpp:29: error:...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.