Which of the following is faster?
Method 1:
int foo (int A, int B) {
// write equations
}
Method 2:
int operator| (int A, int B) {
// write equations
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They are both a function (and thus have no difference in speed), just that the one (operator) can use a more “fancy” syntax to be invoked.
Your question when deciding what of the two to use should not be the speed, but rather if the meaning is natural to the type you use the operator on. When you use it to do something that would be totally surprising to the reader, considering he knows what the operator normally does, then don’t overload the operator.
(I am assuming here you mean your own type instead of int, and just in case you didn’t know, you can not overload operators on ints, one of the parameters for an overloaded operator must be a user defined type)