answersLogoWhite

0


Best Answer

You pass parameters by value when you do not want the original value to change. Passing by value creates a temporary copy of the value, only the copy is affected, but it can add some overhead to the function call, especially if the value is a complex structure or object. If you want any changes reflected in the original value, you must pass by reference instead.

However, if the parameter is a constant reference, the object's immutable members are not altered. So when the option exists to pass by value or by constant reference, choose constant reference every time. Pass by non-constant reference only when you expect any changes to be reflected in the original value, otherwise pass by value.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Primarily, the advantage is to exploit the intuition of the user. After all, there is no point whatsoever in providing an operator overload if it cannot be used intuitively.

For instance, when you increment a pointer, you expect the pointer's address to increment by sizeof(*p), not sizeof(p). That is, your intuition tells you it must increment by the size of the type being pointed at (*p), not by the size of the pointer itself (p). Were it to return the latter, it would far from intuitive and would be ultimately useless to the user -- the programmer. Thus when overloading operators you must keep this principal foremost in your mind. If the result of an operation cannot be regarded as being intuitive, it is better to simply not provide one.

As another example, the addition or plus operator (+) is intuitively used to provide the sum of two numeric objects. But it can also be used to return the sum of two strings (appending the r-value to the l-value to return a new string). This has become so intuitive to programmers that they do not question why they cannot also subtract, multiply or divide strings. It is simply because these are non-intuitive operations when applied to strings. Again, keep this in mind when tempted to provide arithmetic operators to your objects. If they contain mixed data (numeric values, strings and other complex objects), then you would not expect to perform arithmetic at all, so leave them out. If the objects contain purely numeric data, then arithmetic could be considered intuitive in some cases, but certainly not all cases. If in doubt, leave it out.

Most of the time the only operators you really need to overload are the assignment operator (=) and the relational operators (==, !=, <, <=, > and >=), thus allowing users to compare objects intuitively. Indeed, when sorting objects you're really only interested in the less-than operator, but if you provide one relational operator then you are always expected to provide them all, because users will intuitively expect them all.

Keep in mind that any operation can be handled by a named function instead. Indeed, most operations should simply delegate their implementation to an equivalent function. Named functions are not as intuitive as operators, but they are more explicit -- assuming the name is self-explanatory. And if you cannot provide an intuitive operator, then a named function is a much safer option. For instance, string objects typically support the compound += operator and an append method. Both do exactly the same thing and while append is explicit and self explanatory, the += operator is more intuitive. Thus the += operator simply invokes the append method. Moreover, the += operator can be inline expanded to remove the otherwise unnecessary function call.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantages of operator overloading in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


Why do you use of operator overloading in c?

Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...


How do you find largest of two object using operator overloading in c?

I will not use operator overloading in C# to do anything. Operator overloading may lead to one operator has more than 1 semantic meaning. For example, we know 1 + 2 yields 3, and "1" + 2 yields "12". I do not like this overloading of the operator + being used for addition in Number hierarchy, while as the concatenation in strings. That is, one operator (+) has 2 significant semantics.And the question "find largest of two object" is too vague - what do you mean "largest"? and object? We know apple and orange are 2 objects, but how do you compare them, and find the largest one?????? (size, price or what???)


What is the operator is used to allocate the memory in c plus plus?

calloc operator,malloc operator


What is operator overloading?

I think you mean operation overlord??? It is the American, Canadian and British offensive on Europe in World War 2. They landed in Normandy on 6th June 1944 (Commonly called D-Day, Day of Days or Deliverance Day) and progressed throughout France liberating Paris on the 25th August. This allowed the allies a foothold in Europe.

Related questions

Is it possible to do operator overloading in c?

No. Operator and/or function overloading is only a C++ thing.


C coding for operator overloading?

C does not support operator overloading. If you mean C++ operator overloading, it depends on exactly what you wanted to do. If you wanted to '+' to strings, then you could write: string operator+(string a, string b) { // do something }


Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


What is an operator overloading in C?

one function but multiple behaviours depending on the parameters


Why should you have to use comparison operator overloading in pairs in c?

You cannot overload operators in C. This is a C++ thing only.


Why do you use of operator overloading in c?

Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...


What is the operator that cannot be overloaded in c plus plus and java?

conditional operator , size of operator , membership operator and scope resulation operator can not be overload in c++


How do you find largest of two object using operator overloading in c?

I will not use operator overloading in C# to do anything. Operator overloading may lead to one operator has more than 1 semantic meaning. For example, we know 1 + 2 yields 3, and "1" + 2 yields "12". I do not like this overloading of the operator + being used for addition in Number hierarchy, while as the concatenation in strings. That is, one operator (+) has 2 significant semantics.And the question "find largest of two object" is too vague - what do you mean "largest"? and object? We know apple and orange are 2 objects, but how do you compare them, and find the largest one?????? (size, price or what???)


What is the operator is used to allocate the memory in c plus plus?

calloc operator,malloc operator


What is the operator of power in c plus plus?

There is no "power" operator in C or C++. You need to the use the math library function pow().


Example of binaray operator in c plus plus?

+ is an example, one of many, of a binary operator in C or C++ a = b + c; // for usage example


What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.