English 中文(简体)
Const correctness in C++ operator overloading returns
原标题:

I m a little confused as to why I ve been told to return const foo from a binary operator in c++ instead of just foo.

I ve been reading Bruce Eckel s "Thinking in C++", and in the chapter on operator overloading, he says that "by making the return value [of an over-loading binary operator] const, you state that only a const member function can be called for that return value. This is const-correct, because it prevents you from storing potentially valuable information in an object that will be most likely be lost".

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

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 this sort of assignment, wouldn t it make sense to have the + operator return a non-const value? This could be done by adding const_casts, but that gets pretty bulky, doesn t it?

Thanks!

最佳回答

When you say ++x, you re saying "add 1 to x, store the result back into x, and tell me what it was". This is the preincrement operator. But, in ++(a+b), how are you supposed to "store the result back into a+b"?

Certainly you could store the result back into the temporary which is presently holding the result of a+b, which would vanish soon enough. But if you didn t really care where the result was stored, why did you increment it instead of just adding one?

问题回答

FYI, ++(a + b) is illegal even with PODs (plain old data types, like int). So it makes sense not to allow it for your own class types either. Try this:

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 make your copy constructor take a const Integer argument, and create your Integer c like this instead:

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

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

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

I believe OP s example would be suitable to the question if the addition operator is substituted with any other binary operator that returns a reference, for example an assignment operator:

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

I came here wondering whether I should make my assignment operator return a const or a non-const reference. Some tutorials use non-const versions, contrary to "Thinking in C++" s advice. And some other references give reasoning behind that:

Notice that the returned reference is not declared const. This can be a bit confusing, because it allows you to write crazy stuff like this:

MyClass a, b, c;

...

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

At first glance, you might want to prevent situations like this, by having operator= return a const reference. However, statements like this will work with primitive types. And, even worse, some tools actually rely on this behavior. Therefore, it is important to return a non-const reference from your operator=. The rule of thumb is, "If it s good enough for ints, it s good enough for user-defined data-types."





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签