Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Quote:
When I read my C++ book,I can't clearly understand why operator () should be overloaded,how do you think about operator ()?


What I have tried:

When I read my C++ book,I can't clearly understand why operator () should be overloaded,how do you think about operator ()?
Posted
Updated 6-Jun-16 8:05am
Comments
Philippe Mori 6-Jun-16 19:12pm    
Why you write same text twice?
Philippe Mori 6-Jun-16 19:21pm    
The main reason to overload that operator is to create a function object. Often this will be used as an alternative to calling a function with a pointer to the function. See std::function for example.

Your trying to overload the function call operator, "()". It's normally used to create support for a variable number of arguments in a function call.

Example[^].
 
Share this answer
 
First of all, this is one of the advanced C++ features. If you are a beginner, I would advice to check yourself if you are quite comfortable with "the rest of C++". If not, you can safely post-pone the operator overloads to a later time: the feature is advanced and not critically important to anything.

You should understand: there are no technical or code design issues related to the use of overloaded operators, including (). There is just a problem of syntax of using one or another overload, which is simply solved by following C++ reference manual. You need to understand: the operator overloading feature is just the syntactic sugar. It is not a problem or requirement, it is just the possibility.

In this respect, the question "why the operator should be overloaded" is not quite correct. It should not, it can be. I understand that you are looking for some rationale behind it. But there is no much of rationale (unlike, say, deeply technological aspects, such as virtual mechanism, dynamic dispatch) except your own idea of how you can use to make some C++ API you produce intricate, elegant and compact. Let's assume, your real question would be (I mean it, "would be", I'm not sure about your actual motivation): "why should we use () operator overloads if we can do everything without it". Then the valid answers would be: "You are right, why indeed? as you don't really need it, don't use it" or "Why not?".

Please see, for example, Function call operator () overloading in C++.

As you can see, the () operator call here mimics the constructor call. You could also have a static function returning the instance of the declaring class, but it would require a separate name. If you need more than once signature for the methods creating instances (often called "factory methods"), you wold have several named functions, possibly "overloaded", but () "saves" you from inventing extra names.

Now, here is a warning for you. Indeed, you can have some API more elegant with operator overloading. But it's much easier to confuse the user of your code. As I remember, in some books by Bjarne Stroustrup, the big part of the sections on operator overloading is devoted to the warning against wild operator overloading use. For a user, "a new way to call a function" (the expression from the article referenced above), a usual C++ syntax is always the most natural and clear.

The approach should come from the functionality of some class interface you want to create. You may think: can I improve the usage of my C++ code? Can I use this or that, overload [] or ()? Aha! Here is the idea…

—SA
 
Share this answer
 
Comments
Member 12408248 6-Jun-16 22:11pm    
You must be very exciting about operator overloading.Thanks for your suggestion and encouragement.
Sergey Alexandrovich Kryukov 6-Jun-16 22:52pm    
You are very welcome.
No, I'm not excited about that, and not about anything related to C++. Just the opposite, my main point is the advice to use some critical thinking and skepticism.

Good luck, call again.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900