const member function depreciated....

Started by
20 comments, last by snk_kid 19 years, 6 months ago
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?
Author Freeworld3Dhttp://www.freeworld3d.org
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'.
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.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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.
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
Author Freeworld3Dhttp://www.freeworld3d.org
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.
"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


What's the point3?
"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
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.
Things change.
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&.
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.

This topic is closed to new replies.

Advertisement