# const member function depreciated....

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

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

• 18
• 29
• 11
• 21
• 16
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!