sav_a
sav_a

Reputation: 121

operator overloading VS template function

I want to write a program that multiplies two 2d arrays. Both arrays are instances of the template class( arrays can be int, float, double). Is it better to overload operators * and = , or to write a function that will multiply arrays? What are the advantages and disadvantages of operator overloading? Does overloading affect the performance of the program?

Upvotes: 0

Views: 242

Answers (1)

Adi Levin
Adi Levin

Reputation: 5243

Overloading operators does not have any performance penalty. It translates to a regular function call. The advantage of operator overloading is only that it makes your code shorter. However, in case of array multiplication, I recommend to use a properly named function because there are at least two kinds of multiplication semantics for vectors - there is element-wise multiplication, and there is dot-product a.k.a. scalar multiplication. An overloaded operator* will leave the meaning obscure.

Upvotes: 0

Related Questions