optimization - C++ Operator overloading styles, which is better for optimisation? -
there 2 ways overload binary operators in c++: member function, or non-member function.
since member function style can included in .h file, , non-member style in .cpp file, have thought member function style make easier compiler optimise.
- is there difference in performance between member , non-member operators?
- do member , non-member operators result in identical code (in compiler)?
the declaration member or not member has nothing optimization. compiler smart enough produce equally optimized code in both cases if changes. , anyway, low level optimization make sense precision compiler, version , configuration , can change one.
for question of readability , maintanability, operator should member... unless cannot. if can, allows declare inside class definition coherent oop: operates on objects of class, let's try make method on class.
simply there cases not possible. example, output streams injector (<<
operator) can accept second operands of class, cannot defined member. that's reason why c++ allows non member definition of operators.
Comments
Post a Comment