Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inlining an overloaded operator c++

Can/should an overloaded operator have to be inlined to gain better efficiency (wrt time or whatever) if that operator will have to be used frequently?

I want to overload the '+' operator to add big vectors very frequently in my code. Hence the question.

like image 818
smilingbuddha Avatar asked Sep 22 '11 20:09

smilingbuddha


People also ask

Can you overload an operator in C?

C does not support overloading of operators or functions. There's no way you can redefine < , <= , > , >= , == , or != to compare struct types directly.

What is the syntax to overload an operator?

When we overload the binary operator for user-defined types by using the code: obj3 = obj1 + obj2; The operator function is called using the obj1 object and obj2 is passed as an argument to the function.

What is operator overloading with example?

This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.

How binary operators are overloaded?

Overloading Binary Operator: In binary operator overloading function, there should be one argument to be passed. It is overloading of an operator operating on two operands. Let's take the same example of class Distance, but this time, add two distance objects.


2 Answers

Ideally, you'd profile your code and then decide what to inline. There really isn't much of a difference between when you decide to inline regular operators, over overloaded ones.

like image 90
blueberryfields Avatar answered Oct 24 '22 08:10

blueberryfields


If you are adding big vectors, then the overhead of a function call to the plus will be small relative to the time to actually add the two vectors. So marking the operator+ inline is not likely to improve your overall run time.

like image 45
David Nehme Avatar answered Oct 24 '22 09:10

David Nehme