Sign in to follow this  
soconne

const member function depreciated....

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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


What's the point3?

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
I tried to compile the code with DJGPP and got this result:


C:\TEMP>gpp -O -o point.exe point.cc
point.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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 this post


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



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

Share this post


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



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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this