() isn't a operator. Do you mean function-overloading?
int foo(int bar)
{
return bar*2;
}
float foo(float bar)
{
return bar*2.0f;
}
In the above example, I've created two functions with the same name and functionality. But one is for ints and one is for floats => function overloading
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
If you are asking "why is overloading functions useful", then the answer is because sometimes we want two or more functions with the same name which provide the same functionality, but which require different parameters.
Lets take an actual example and look at the append() function on std::string. Here are a few of the overloaded functions:
string& append ( const string& str );
string& append ( const char* s );
string& append ( const char* s, size_t n );
All three of these functions have the same name and do basically the same thing - they append something to a string. So why do we need more than one version? Well assume you want to append another string to your string, then you would call the first version which takes the string you want to add on as a parameter. But wait, what if you have an array of char instead of a string? Well, just use the second version of the function which takes a pointer to char. But what if you just wanted to append part of the char* string? Then use the 3rd version which takes the char* parameter as well as the number of characters that should be appended.
mahlerfive
Junior Poster in Training
77 posts since Aug 2008
Reputation Points: 33
Solved Threads: 18
I need to know Why there is need to operload () this.
STL is one reason, some discussion/examples here
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
When you overload the () operator, it is called the conversion operator. It provides explicit instructions as to how to implicitly convert a class object into a built in plain old data type. At least that's how my reference explains it.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
>At least that's how my reference explains it.
You might be misunderstanding your reference. There's an implicit conversion operator and a function call operator The function call operator is a general member function in terms of return value and parameters. The implicit conversion operator takes no parameters ( *this is implied) and the return value of of the type the operator defines. Non-explicit copy constructors and assignment operators give you an implicit conversion in the opposite direction (from whatever type to the type of the class):
#include <iostream>
class foo {
double base;
public:
// Implicit copy constructor (type T -> new type foo)
foo ( double d )
: base ( d )
{}
// Assignment operator (type T -> existing type foo)
foo& operator= ( double d )
{
base = d;
return *this;
}
// Function call operator (does whatever you might want)
std::ostream& operator() ( const char *s )
{
return std::cout<< s <<'\n';
}
// Implicit conversion operator (existing type foo -> type T)
operator double()
{
return base;
}
};
int main()
{
foo f = 123.456;
std::cout<< std::boolalpha << !!f ( "This is a test ") <<'\n';
std::cout<< f <<'\n';
f = 987.654;
std::cout<< f <<'\n';
}
Basically the function call operator gives you the option of making an object act like a function. Hence, the "functor" or function object that jencas mentioned.
>() isn't a operator.
Yes, it is. [] is an operator too, and , is an operator as well, sometimes to the surprise of even experienced C++ programmers.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Actually my reference doesn't indicate specifically that the conversion operator is an overloaded version of the () operator. In discussing the conversion operator, however, it indicates that "ALL (the reference's emphasis, not mine) operator overloading works the same---you declare an overloaded operator using the keyword operator" which is consistent with the example provided:
class Counter
{
public:
operator unsigned short()
{
return (int(itsval));
}
private:
int itsVal;
};
It doesn't really state whether the operator is () or, in this case, whether the operator is unsigned int (). Since () is an operator and unsigned int is a type, I assumed, perhaps incorrectly, that the conversion operator is an overloaded version of the () operator.
So is the conversion operator not considered overloading the () operator whereas the function call operator is considered overloading the () operator, both are considered overloading the () operator, or neither are considered overloading the () operator?
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
>So is the conversion operator not considered overloading the () operator whereas the
>function call operator is considered overloading the () operator, both are considered
>overloading the () operator, or neither are considered overloading the () operator?
The function call operator and operator() are the same thing. Here's how you can break it down, and it depends on what goes after the operator keyword:
T operator() ( ... )
{
...
}
This is overloading the () operator because the operator you want to overload follows the operator keyword (operator()). Notice the seemingly redundant parameter lists, which is a good sign that the () operator is being overloaded.
operator T()
{
...
}
This is overloading the implicit conversion operator to type T because the type T follows the operator keyword (operatorT). A space is required between the two so that the construct isn't treated as an identifier. The () in that is the parameter list, not the operator to overload.
Your book is correct, but it sounds like it's trying to be too clever in the explanation and causing confusion.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
So the conversion operator is an operator without a symbol (that is no & or || or << or []) to represent it. Ha.
Thanks for the education!
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
FindSyntax: Read the welcome guide before posting again. It will learn you to use code-tags and some other handy stuff. Also: posting websites and/or email-addresses is against the rules
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
>So the conversion operator is an operator without a symbol (that is no & or || or << or []) to represent it.
The conversion operator is an operator with a type to represent it, the type being the symbol.
>Operator overloading allows C/C++ operators to have
>user-defined meanings on user-defined types (classes).
Nit: C/C++ only makes sense when the feature/technique/convention is available both to C and C++. Operator overloading is not, nor are classes as defined by C++. Thus, when you say C/C++, you mean C++ and should say C++ to avoid furthering myths about the relationship between C and C++.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401