Correctness of fidelity when overloading a C ++ operator is returned

I'm a bit confused why I was told to return const foo from a binary operator in C ++ instead of just foo.

I read Bruce Eckel's “Thinking in C ++,” and in the chapter on operator overloading, he says that “by making the return value of the [redundant binary operator] const, you declare that only const member function can be called for this return value. This is const-correct because it prevents you from storing potentially valuable information in an object that is likely to be lost. "

However, if I have a plus operator that returns const and a prefix increment operator, this code is not valid:

class Integer{ int i; public: Integer(int ii): i(ii){ } Integer(Integer&); const Integer operator+(); Integer operator++(); }; int main(){ Integer a(0); Integer b(1); Integer c( ++(a + b)); } 

To allow such an assignment, does it not make sense for the + operator to return a value that is not a constant? This can be done by adding const_casts, but it gets rather cumbersome, right?

Thanks!

+4
source share
4 answers

When you say ++ x, you say "add 1 to x, save the result back to x and tell me what it is." This is the preincrement statement. But, in ++ (a + b), how should you "return the result back to + b"?

Of course, you can save the result back to a temporary one, which currently holds the result + b, which will disappear soon. But if you don’t care where the result was saved, why did you increase it and not just add it?

+4
source

FYI, ++(a + b) is illegal even with POD (plain old data types like int ). Therefore, it makes sense to not allow it for your own class types. Try the following:

 int a = 1; int b = 2; int c = ++(a+b); 

GCC returns error: lvalue required as increment operand .

In your case, it would be preferable to make your copy constructor accept the const Integer argument and create Integer c instead:

 Integer c(a + b + Integer(1)); 
+4
source

Copy constructors usually take a const reference, solving this problem for you.

(Having a non-constant copy of ctor implies some transfer of resources, which can be useful sometimes, but for 99% of all situations it is not needed)

0
source

I believe that the OP example would be appropriate for the question if the addition operator is replaced by any other binary operator that returns a reference, for example an assignment operator:

 Integer c( ++(a = b)); 

I came here wondering if I should force the assignment operator to return a constant or a non-constant reference. Some tutorials use non-constant versions, contrary to the "Thinking in C ++" tip. And some other links give reason for this:

Note that the returned link is not declared const. This can be a bit confusing because it allows you to write crazy things like this:

MyClass a, b, c;

...

(a = b) = c; // What ??

At first glance, you can prevent situations like the operator: return = const. However, such statements will work with primitive types. And, even worse, some tools do rely on this behavior. Therefore, it is important to return a non-constant link from your = operator. Rule of thumb: "If it's good enough for ints, it's good enough for custom data types."

0
source

All Articles