-
operator overloading
i have another assignment (the last thank goodness) but here it is...
i have to write the definitions for the functions in a class....these happen to deal w/funciton overloading, i had a similar assignment earlier....am i doing this right? //the first one imparticular
Code:
// prefix ++ operator
Point& operator++(pt)
{
x_ = pt.x_++;
y_ = pt.y_++;
z_ = pt.z_++;
}
const Point operator++(int); // postfix ++ operator
Point& operator-(); // prefix -- operator
const Point operator-(int); // postfix -- operator
-
nope. remember the int passed is a dummy arg to differentiate post and pre.
-
excuse me for being retarded but....huh?
-
The integer passed as an argument has no use to you. It just tells the compiler whether the operator is postfix or prefix.
-
so then how do i make it usefull to me? remove it?
-
ok here we go again post and pre for dummies....
Code:
class plonker
{
private:
int wally;
public:
plonker() : wally(0){}
plonker(int w) : wally(w){}
plonker& operator ++()
{
++wally;
return *this;
}
const plonker operator ++(int)
{
int temp = wally;
++(*this); // implement post in terms of pre for symettry
return temp;
}
};
-
-
getting warmer???
Code:
// prefix ++ operator
Point& operator++()
{
++x_;
++y_;
++z_;
return *this;
}
// postfix ++ operator
const Point operator++(int)
{
int temp = *this;
++(*this);
return temp;
}
-
1) fine
2)crap. try again
-
look im off to bed so one last piece of advice.......
Use the constructor......
-
>> int temp = *this;
think about what 'this' is and what your assigning it to. 'this' is not an int.
-
this should be my 3 ints...right? this is a pointer to x_ y_ and z_ or am i misunderstanding that?
im looking at this web site and it says to just do
++(*this);
am i doing to mucH?
-
The problem is here:
int temp = *this;
this is a pointer, and like all pointers it points to one thing, not three things. What does this point to? this is a pointer to the object you are calling the function on, so *this is the object. The object's type is the class name not int.
"the int passed is a dummy arg to differentiate post and pre."
Try defining post and prefix ++ operators with out using the dummy int:
operator++()
operator++()
Those are exactly the same, so the compiler can't tell the difference. You need int as a dummy argument to differentiate the two:
operator++()
operator++(int)
Since there is no requirement that you have to use an argument that's passed to your function, ignore it in your function definition.
-
well did you get it right?
Code:
const Point operator++(int)
{
Point temp(*this); // call copy con to make copy of current object
++(*this);
return temp;
}
Its that simple.
-
i had
Point temp = *this;
but i went to bed shortly after that, i had another assignment due at midnight i was rushin to get done