Jump to content
  • Advertisement
Sign in to follow this  
soconne

const member function depreciated....

This topic is 5039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!