yes I see it; but why not const? It seem works fine with const too...
yes I see it; but why not const? It seem works fine with const too...
Regards
OK; about const, in this case it seems non influential its presence. Is it right?
Regards
mickey (4th January 2008)
hello,
speaking again about overloading I'm reading "thinking in c++" (online version) and at p507 "Return by value as const" I see when I have to use 'const' keys while overloading. In particular, I've noticed that when I do f(a+b) when f take a value and '+' is overloaded, there's no reason to do 'const' the returned value of operator+: this because the call of 'f' will do 'a+b' automatically 'const' (because a temporary object is a 'const'); probabily I lost something before in the book: what does mean the Bold ?
Thank you
Regards
What Bruce says here is that,
A function is used usually as rvalue, that is its resultant is only used as read operation.
As such the parameter passed "a+b" will result in temporary object which will be an rvalue expression. Since rvalues are always constants, the temporary produced will therefore be const automatically regardless of whether you return const object or not.
However if you use "f( (a+b).someFunc() )" then, the value of "a+b" will be an lvalue if "a+b" returns "non const" object.
By returning const objects, you make sure the resultant of "a+b" is always an rvalue and hence non modifiable. In this case you can only call const functions of the object but not non const functions.
For eg
Qt Code:
int a = 1, b = 2; (a+b) = 4; // invalid because a+b for intergers returns a constant int and hence you cant modify it.To copy to clipboard, switch view to plain text mode
The biggest difference between time and space is that you can't reuse time.
-- Merrick Furst
For me there is an easy rule - If you return a new object (a copy), there is no point in making the object const. When you return the same object (a reference), make it const if you want to forbid direct changes to the returned object (for example if you return a reference only because of overhead related to copying large objects). Otherwise return a non-const reference. There is also another aspect - returning const pointers for example if you want to forbid deleting the object by a 3rd party.
Investigating your case - postfix ++ returns a copy of the object before changes, so no const here. Prefix ++ returns a reference to the incremented object and the object is mutable, thus non const again. If you return a list or another complex structure and you want to force making a copy when the object is to be changed, return a const reference. In this case it is important to mark methods that don't change the object as const to prevent unnecessary copies:
Qt Code:
struct X { int func(){ return 7; } int cFunc() const { return 7; } const X &ref() { return *this; } }; X x; const X &x2 = x.ref(); x.func(); // success x2.func(); // failure - modifying a const object x2.cFunc(); // success /* to call func() on x2 you need to make a copy: */ X x3 = x2; // copy x3.func(); // successTo copy to clipboard, switch view to plain text mode
I'm a little confused of how 'const' work; this below for me shouldn't work: cause 'const' keyword; but it seem works; why?
Could you me explain what 'const' make constant in this case, please?Qt Code:
const Person& operator+=(const Person& right) { //if (this == &right) {/* auto-assignment */ } _age += right._age; return *this; } //main.cpp Person person1("markus"), Person2("Karl"); person1 += person2; //person1 isn't a const;To copy to clipboard, switch view to plain text mode
Regards
The declaration says that operator += takes a const Person& as an argument and returns const Person &. It doesn't say that "this" object should be const.
You can pass non-const objects everywhere where const objects are allowed, because "const" means simply that you can't invoke methods that change the state of the object. It doesn't change the object in any way.
setX() might change the object, so we are not allowed to call, even if constRef is essentially the same thing as obj.Qt Code:
class SomeClass { ... int x() const { return x; } void setX( int x ) { _x = x }; ... }; ... SomeClass obj; const SomeClass & constRef = obj; int x1 = obj.x(); // OK int x2 = constRef.x(); // OK --- x() is marked as const (i.e. it doesn't change object's state) obj.setX( 10 ); // OK constRef.setX( 20 ); // ERRORTo copy to clipboard, switch view to plain text mode
Also:const doesn't say that object won't change --- it says that you can't change it.Qt Code:
int x1 = constRef.x(); obj.setX( 999 ); int x2 = constRef.x(); // != x1To copy to clipboard, switch view to plain text mode
The same applies to const objects.
hello,
refering to my post #10, could you show me any examples of operations (assigment or other) for which at first the returned value of operator+= has to be 'const' and at second, examples for which returned value of operator+= mustn't be 'const' ?
(my problem is 'const' on returned value....)
Thanks.
Regards
I can't think of any situation where the return value of operator+= would have to return a const object. You can do that of course but I just don't see a point.
The last line would be invalid if operator+= returned a const object.Qt Code:
X x; x+=7; // OK x = ++(x+=7); // invalid for const & operator+=To copy to clipboard, switch view to plain text mode
Bookmarks