Overload is a bad thing? Despite support for a variety of new languages, some form of what I always solid, articles, is that the overhead is to say badly. Worse, I find many items that are in any way about the overcharging of bad operators, but that function overloading is absolutely correct. Let’s take a look at the argument.
Bad Example
The following example is often difficult overload.
a = b * c;
This seems innocent enough, right? Now many are quick to point out that in C + +, which could mean something, rather than simple multiplication. You see, you can override the operator to change that. In fact, you can even change what it means equal sign. What at first glance seem a simple multiplication, and could actually be set to be absolutely nothing!
I will be happy to admit that you probably would not do it in C + +, however. In fact, if you have the full significance of the joint operators changed you’re probably a moron. But I do not think the language should be limited only to the functions to prevent the misuse of them crazy. This is simply not possible, some will be an idiot to ever come around and ruin everything. Consider the languages ??allow, function overloading and not the operator. We can write the above as below again.
a.assign (b.mul (c));
Although perhaps not as clear as the first piece of code that no one really sure that the code should do. But what if Mul was not really grow, and what, if not assign a value to assign. Which functions the tube very many thanks for the programmer. In fact, now that I see it is not even anything to do with congestion. A fool can write code in any language with little effort misleading.
Good practice
Overloading operators makes sense in many cases. It is unfortunate that C + + is one of the rare languages ??that provide this function. Personally I find the first bit of code more clear that the second bit, provided that the operator actually as expected. And why not? You see, the possibility of overloading an operator was one to deficiencies in the standard library or types are available. Used properly overload to work with custom types as native types.
Do not think so, look for example. This code assumes that the language can not be overloaded. They have some financial calculations, have a specific question Decimal class, but still need some work with integers and floating point.
Apply decimal (decimal)
{R = Decimal base.add_int (1);
r.mul_float r = (3,5);
base.sub_decimal return (r);}
It just seems like typing a lot of things you should know already, the compiler. Is not it just seem clearer to write the code below?
Dual-use (double base)
{Double r = base + 1;
r = r * 3.5;
Base back – r;}
The purpose of this code is clearer than in its previous form. You might think that these kind of overloading of a rarity, but that’s simply not true. Between the complex numbers, rational, accurate financial, stock ticks, astronomy, and any number of special applications, you never can predict what the numeric types are needed. In the following, so many kinds of collection that are used to make the notation index. Or maybe just a few items of data to be nice, with less-than comparison would be. Overloading operators can work in the right hands, with specialized types make it much easier.
Completion
I agree that the obscure code by the meaning of the operators a bad idea. But I hope that my little example can show that when used properly, it can be very positive. It should also be clear that we never poor protection of the languages ??programmers, they always find a way to wreak havoc. Argument that the function can be redirected, is simply a non-starter. Check out the features of the language and use them all appropriately. It is also unfortunate that only very few have the full support of congestion.
0 ResponsesLeave a comment ?