I did not see anything that address my particular code problem.
I have a bool function in a class; the bool function is named:
bool Triplet::operator ==(const Triplet& operand)const {
if( (data[0] == operand.data[0]) &&
(data[1] == operand.data[1]) &&
(data[2] == operand.data[2]))
return true;
...
And I am trying to call it in Main but having problems just getting the call right. Apparently if I leave out any thing in the wording it gives an error that I have too few aruguments and if I try to use the entire wording of the function, I get the error that I need a semi-colon, but I already have a semi-colon at the end of the call, so I know that something else is wrong and I simply cannot figure out what is wro开发者_JAVA百科ng! Any help would be appreciated!
Thanks in advance.
You can call it using ==
(that's what operator overloading does; it overloads the meaning of the operator):
Triplet a;
Triplet b;
if (a == b) //< calls your operator==
You can also call it using the function call syntax:
if (a.operator==(b)) //< also calls your operator==
but you don't usually want or need to do that.
One case you might need to do that is if for some reason you have an operator template that has template parameters that can't be deduced from the arguments (that's rare for operator==
, but I've seen that used for operator[]
before).
That is a C++ operator overload, it is designed so it can change the behaviour of the normal operator:
Triplet a, b;
if (a == b) // calls Triplet::operator==()
If you really want to, you can call the operator by name:
if (a.operator==(b))
bool
is just the return type. It has nothing to do with how the function as called. As in James' example, this function is an operator, therefore you will use the operator like you would in normal syntax to call it.
精彩评论