const member function depreciated....

Started by
20 comments, last by snk_kid 19 years, 6 months ago
Quote:Original post by oconnellseanm

point a, b;
point &func(const point &a, const point &b)
{
point result = a + b;
return result;
}


I get an error for the line "point result = a + b"
Why can I not add 'a' and 'b'. I know they are constants, but is there a way to get around this?


Are you sure the errors with "point result = a + b"? The only thing that I can see wrong with that code is that it returns a reference to a local variable, which should give you a warning.
Advertisement
Quote:Original post by Oluseyi
Quote:Original post by oconnellseanm
i have the following code
First off, defining the return type of your addition and subtraction overloads as a const non-reference means that they can not be chained (a + b + c - d would fail to compile).

The rest is a type mismatch.


I don't think that's right. A const non-reference is the correct way to return a value from operator+(). If the returned value is non-const then the result of operator+() to be used as an lvalue which is inconsistent with the the way it behaves for primitives.

i.e. (a + b) += c; // Illegal for primatives
Quote:Original post by Boku San
Also, compiler copy constructors don't ever work right in my experience...so I agree with Mr. Copy Constructor over there.


Really, even for something as primative as copying 3 doubles? What compiler are you using?
Quote:Original post by Oluseyi
Quote:Original post by oconnellseanm
i have the following code
First off, defining the return type of your addition and subtraction overloads as a const non-reference means that they can not be chained (a + b + c - d would fail to compile).

Sure they can. The const temporary that gets returned is passed into operator+(const point&) const as this, What's the problem?
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Quote:Original post by oconnellseanm
Here's the exact error from DevC++

189 C:\Documents and Settings\Sean\Desktop\main.cpp passing `const point' as `this' argument of `point point3::operator+(const point&)' discards qualifiers


Have you left the const off of "const point operator +(const point &p) const" in your code? That's the error messsage you get when you try to call a non-const member function on a const object.
Quote:Original post by Extrarius
Posting the exact error would be helpful. I'm going to have to disagree with nmi about the const: I've started labeling all return types const since they should be and I've had no such trouble.


Actually there is no gain from declaring the return value constant because first an expression such as:

foo a = b + c;


where the return type of the overloaded operator returns a constant foo, if the compiler doesn't optimize it will actually create an extra redundant copy because the type of the variable A in the assigment is not a constant type.

secondly the compiler will ignore the fact that the return type is constant or not where immediate results are generated it will always generate a temporary that you can only invoke constant methods of the type in question e.g.

foo a = b + c * d;


so the temporary generated by (c * d) can only invoke constant methods.

So in the end there is no gain from declaring user-defined return type constant and may actually lead to redundant copying if not optimized by the compiler.
Quote:Original post by snk_kid
Actually there is no gain from declaring the return value constant because first an expression such as:

foo a = b + c;


where the return type of the overloaded operator returns a constant foo, if the compiler doesn't optimize it will actually create an extra redundant copy because the type of the variable A in the assigment is not a constant type.


I'm not sure I understand. Are you saying that it wmight not perform the return value optimization if you try to assign a const to a non-const?

Quote:
So in the end there is no gain from declaring user-defined return type constant and may actually lead to redundant copying if not optimized by the compiler.


But isn't the point to declaring the return value as const to prevent the result being used as lvalue? i.e. (a + b) += c shouldn't be valid.
Quote:Original post by Boku San
compiler copy constructors don't ever work right in my experience...


What experience is that? Can you give an example?

compiler copy constructors are well defined in their behaviour. If you know what it is then you won't have a problem.
Quote:Original post by Kaijin
Quote:Original post by snk_kid
Actually there is no gain from declaring the return value constant because first an expression such as:

foo a = b + c;


where the return type of the overloaded operator returns a constant foo, if the compiler doesn't optimize it will actually create an extra redundant copy because the type of the variable A in the assigment is not a constant type.


I'm not sure I understand. Are you saying that it wmight not perform the return value optimization if you try to assign a const to a non-const?


Yep, you end up making two copies but compilers are not as dumb as humans and will optimize the extra copy away.

Quote:Original post by Kaijin
Quote:
So in the end there is no gain from declaring user-defined return type constant and may actually lead to redundant copying if not optimized by the compiler.


But isn't the point to declaring the return value as const to prevent the result being used as lvalue? i.e. (a + b) += c shouldn't be valid.


Thats a good reason to declare it as constant then it will behave as built-in types.
Pedantic note: the word from the error is "deprecated", meaning "disapproved of", "pled earnestly against" (according to my copy of the COD), "discouraged as no longer being a good way to do it" (according to how I normally understand it and how the compilers use it). "depreciated" means "decreased in value", "belittled", "reduced in purchasing power" (when referring to currency).

Getting these kinds of things right is an important part of sounding authoritative. ;)

This topic is closed to new replies.

Advertisement