May 12, 2012 at 8:58pm UTC
Hello,
When operator overloading, are all parameters in each operator (operator+, operator-, etc) pre-defined?
~Flurite
May 12, 2012 at 9:03pm UTC
...predefined as in there's a limit on the types of the arguments they can take, or as in something else?
-Albatross
May 12, 2012 at 9:04pm UTC
The types and the number of arguments.
May 12, 2012 at 9:05pm UTC
I'm not sure what you mean by pre-defined.
I believe the only things that are predefined are
a) The number of arguments each operator takes (+ - * / = % all take 2, ++ -- take 1, ?: takes 3)
b) The behavior with the basic types are defined (int + int, double + int, etc.)
Here's a good reference regarding how to overload each operator:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
Last edited on May 12, 2012 at 9:06pm UTC
May 12, 2012 at 9:07pm UTC
@Stewbond,
Thanks. I am assuming the type is pre-defined then as well, from what you say in b?
May 12, 2012 at 9:11pm UTC
What I'm saying in b is that you don't have to define behavior for int + int. It's already done. But if you have your own class, then you will need to do it yourself.
May 12, 2012 at 9:11pm UTC
Not at all! What Stewbond was saying is that the operators are all already defined for all the basic types and for operations using only basic types.
If something more complex like a class is involved you're free to specify your own types.
EDIT: Neenjah.
-Albatross
Last edited on May 12, 2012 at 9:11pm UTC
May 12, 2012 at 9:16pm UTC
Ahh, I see.
Answer: Overloading operators for classes allows you to specify the types of the parameters, but the number of parameters is set.
That right?