# const member function depreciated....

## Recommended Posts

soconne    105
i have the following code

class point
{
public:
inline const point operator +(const point &p) const;
inline const point operator -() const;
double x, y, z;
};

inline const point point::operator +(const point &p) const
{
point result;
result.x = this->x + p.x;
result.y = this->y + p.y;
result.z = this->z + p.z;

return result;
}


For some reason if I do the following code I get errors in DevC++ talking about depricated values 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?

##### Share on other sites
nmi    978
I think the problem is not adding the two values, but storing the result.

The operator+ returns a 'const point', but result is defined to be 'point'.

##### Share on other sites
Extrarius    1412
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.

##### Share on other sites
Deyja    920
Quote:
 The operator+ returns a 'const point', but result is defined to be 'point'.

Irrelivant. You are simply missing an operator =. You should also have a copy constructor.

##### Share on other sites
soconne    105
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

##### Share on other sites
joanusdmentia    1060
Quote:
Original post by Deyja
Quote:
 The operator+ returns a 'const point', but result is defined to be 'point'.

Irrelivant. You are simply missing an operator =. You should also have a copy constructor.

Irrelivant. The compile will generate a default assignment operator and copy constructor that performs a shallow copy.

##### Share on other sites
joanusdmentia    1060
Quote:
 Original post by oconnellseanmHere'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

What's the point3?

##### Share on other sites
Boku San    428
Irrelevant. You guys were spelling it wrong. Also, compiler copy constructors don't ever work right in my experience...so I agree with Mr. Copy Constructor over there.

##### Share on other sites
nmi    978
I tried to compile the code with DJGPP and got this result:

C:\TEMP>gpp -O -o point.exe point.ccpoint.cc: In function point& func(const point&, const point&)':point.cc:22: warning: reference to local variable result' returned

where line 22 contains:
point result = a + b;

That is because the reference returned will point to the local variable that does not exist anymore when returned.

You should better define func to return a point instead of a point&.

##### Share on other sites
Oluseyi    2103
Quote:
 Original post by oconnellseanmi 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.

##### Share on other sites
Kaijin    163
Quote:
 Original post by oconnellseanmpoint 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.

##### Share on other sites
Kaijin    163
Quote:
Original post by Oluseyi
Quote:
 Original post by oconnellseanmi 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

##### Share on other sites
Kaijin    163
Quote:
 Original post by Boku SanAlso, 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?

##### Share on other sites
joanusdmentia    1060
Quote:
Original post by Oluseyi
Quote:
 Original post by oconnellseanmi 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?

##### Share on other sites
Kaijin    163
Quote:
 Original post by oconnellseanmHere'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.

##### Share on other sites
snk_kid    1312
Quote:
 Original post by ExtrariusPosting 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.

##### Share on other sites
Kaijin    163
Quote:
 Original post by snk_kidActually 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.

##### Share on other sites
petewood    819
Quote:
 Original post by Boku Sancompiler 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.

##### Share on other sites
snk_kid    1312
Quote:
Original post by Kaijin
Quote:
 Original post by snk_kidActually 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.

##### Share on other sites
Zahlman    1682
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. ;)

##### Share on other sites
Kaijin    163
Quote:
Original post by snk_kid
Quote:
 Original post by KaijinI'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.

Ah, ok. That's interesting. So generally it's safe to assume the compiler will take care of it?

##### Share on other sites
snk_kid    1312
Quote:
Original post by Kaijin
Quote:
Original post by snk_kid
Quote:
 Original post by KaijinI'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.

Ah, ok. That's interesting. So generally it's safe to assume the compiler will take care of it?

Yeah, i know gcc & VC++ 7.1 does.