++ operator overloading
I would like to overload the ++ operator. I did it for pre-fix (i.e. ++myclass) but I don''t understand how to overload the postfix++ it (i.e. myclass++). Thanks for your help,
Mike
The ++ pre-fix and post-fix operators are the same, the only difference is that with the postfix first the current command is exexcuted and then the ++ operator, with the prefix it''s the other way round. There shouldn''t be a difference with overloaded operators.
Visit our homepage: www.rarebyte.de.st
GA
Visit our homepage: www.rarebyte.de.st
GA
Sorry ga, there is a difference...
The prefix version:
aclassfunction operator++ ()
{
}
The post-fix version:
aclassfunction operator++(int)
{
}
The ''int'' is just a dummy argument, thats it. Very subtle but very important!
Cheers
Matt
Check out my project at: www.btinternet.com/~Matthew.Bennett
The prefix version:
aclassfunction operator++ ()
{
}
The post-fix version:
aclassfunction operator++(int)
{
}
The ''int'' is just a dummy argument, thats it. Very subtle but very important!
Cheers
Matt
Check out my project at: www.btinternet.com/~Matthew.Bennett
DataType operator++ ( DataType& a )
{
// prefix
a.item++;
return a;
// postfix
DataType temp = a;
a.item++;
return temp;
}
{
// prefix
a.item++;
return a;
// postfix
DataType temp = a;
a.item++;
return temp;
}
3dModelMan is correct: the postfix operator++ needs a dummy argument of int. The prefix operator++ doesn''t need it.
Make sure that you''re returing the correct value: the postfix operator++ should return the value before the increment, the prefix operator++ should return the value after the increment. Of course you''re allowed to use different semantics, but it''ll confuse the users of your code.
Erik
Make sure that you''re returing the correct value: the postfix operator++ should return the value before the increment, the prefix operator++ should return the value after the increment. Of course you''re allowed to use different semantics, but it''ll confuse the users of your code.
Erik
quote:Original post by Kaellaar
Anonymous,
++ is a unary operator, that function won''t work.
Is it not possible to declare it that way if you made it a friend function of the class rather than a member?
I''m sorry for my wrong answer. I never needed overloading the ++ operator so I really didn''t expect that there would be a difference to the usual ++ operator.
Visit our homepage: www.rarebyte.de.st
GA
Visit our homepage: www.rarebyte.de.st
GA
I don''t think so, I may be wrong, but I dont'' even think the compiler will let you put anything but an int as a parameter.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement