Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi, I was reading my C++ book which a gave a very little information about the () operator - He said that's not very important, But I'm curious to know what is this operator, maybe I'll use it someday.

Thanks, Sam
Posted
Comments
Mohibur Rashid 9-Jul-15 21:55pm    
keep reading the book
Samuel Shokry 9-Jul-15 22:20pm    
Actually I have finished reading it.
Samuel Shokry 9-Jul-15 22:23pm    
It is Practical C++ programming by Steve Oualline. He wrote exactly 3 lines about it plus 7 lines of simple example that I haven't understand well.

You can think of the () operator as a classs method without a name, because that is (almost) how it looks when you invoke this operator: rather than
C++
myobject.foo();
myobject.bar(1, 3.13, "hello world");

the calls would look like this:
C++
myobject();
myobject(1, 3.13, "hello world");

Note that the '.' is missing; this is on purpose, because the idea is to use the class object like a function. The main use of this operator is in functional programming (remember, C++ is not an object oriented language, it is a general purpose language, and therefore suitable for any programming paradigm!), and in generic programming.
 
Share this answer
 
 
Share this answer
 
This is a functional form or function-call operator or conversion (C-style cast) operator. I have no idea how a book can explain it all in 7 lines, and I have no enthusiasm to find out. Use something decent:
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B[^],
http://www.cplusplus.com/doc/tutorial/operators[^],
http://www.tutorialspoint.com/cplusplus/function_call_operator_overloading.htm[^],
http://www.cplusplus.com/doc/tutorial/typecasting[^],
https://msdn.microsoft.com/en-us/library/df74sak1.aspx[^],
https://msdn.microsoft.com/en-us/library/ts48df3y.aspx[^].

—SA
 
Share this answer
 
Comments
Stefan_Lang 10-Jul-15 3:57am    
I disagree about C-style cast operator - I'm sure you'll agree if you think about it for a moment. ;-)
Sergey Alexandrovich Kryukov 10-Jul-15 10:13am    
I just mean: https://msdn.microsoft.com/en-us/library/ts48df3y.aspx (referenced above). Please, what are you disagree with?
Consider it is not "()", or what?
—SA
Andreas Gieriet 10-Jul-15 15:48pm    
I agree with Stefan.
If talking about () operator, you usually mean the function call operator SomeTypeT operator()(some parameters ...) { ... }, I'd say.
The cast operator (if called is of the form (SomeTypeT)expr) is defined as operator SomeTypeT () { ... }.
Regards
Andi
Sergey Alexandrovich Kryukov 10-Jul-15 19:39pm    
You are right, of course, but where did I say anything against it?
The question is about '()', without naming it anyhow; I just added some information about cast operator which can be defined as
operator const char *()
I thought that, in a way, it has something to do with '()' :-)
Where are you disagree, exactly?
—SA
Andreas Gieriet 12-Jul-15 9:15am    
What about the placement-new operator? E.g. for a memory pool void* operator new(std::size_t, Pool*) { ... } or alike and call it MyClass *obj = new (myPool) MyClass;?
Just since the operator has some parenthesis in the expression does not mean that it has to do with the () operator... ;-)
So, I disagree that the cast operator has anything to do with the () operator.
Cheers
Andi

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