C++: Operator Overloading (slightly more complex)
How to Overload the operators: ++(int), (int)++, --(int), (int)--
The key is to know the action of these operators on the integers they are supplied for:
For coding examples i will again be using Person as my example class. Perhaps ++ or -- increments or decrements the number of friends a particular person has.
ex:
int i = 0;
++i = 1;
i++ = 1; //result is old value
i = 2; //next time i is used, reveals the new value
--i = 1;
i-- = 1; result is still old value
i = 0; //next time i is used, reveals the new value
No parameters are supplied in either case the prefix versions are straight forward:
Prefix ++ | ++(int):
//++Operator Overloader - prefix
Person& operator++(){
this->friends = this->friends+1; //assuming Person class has a field friends.
return *this; return in the case of an assignment as well
}
Prefix -- | --(int):
//--Operator Overloader - prefix
Person& operator--(){
this->friends = this->friends-1; //assuming Person class has a field friends.
return *this; return in the case of an assignment as well
}
*You may also wish to check for a result of this->friends == 0 in this method.
The postfix version may not appear so obvious:
Postfix ++ | (int)++:
//++Operator Overloader - postfix
Person& operator++(int){ //the int here specifies as postfix
//the int is NOT a parameter
Person *p = new Person(*this); //requires a copy constructor
//p is now a copy of the given Person
this->friends = this->friends+1; //update this Person
return *p; return copy before modification
}
*We must return the copy for use and the postfix requires the object be changed, but the original returned.
Postfix -- | (int)--:
//--Operator Overloader - postfix
Person& operator--(int){ //the int here specifies as postfix
//the int is NOT a parameter
Person *p = new Person(*this); //requires a copy constructor
//p is now a copy of the given Person
this->friends = this->friends-1; //update this Person
return *p; return copy before modification
}
*We must return the copy for use and the postfix requires the object be changed, but the original returned.
Slightly more thinking, but relatively straight forward, enjoy!
Question/Comments: william_a_wilson@hotmail.com
-William. (marvin_gohan)
Also see ...
H3How to Overload the operators: +=, =, *=, /=, %=/H3PVery similar to overloading the +, ,* operators within a class, as they all take a single parameter as such: br / br /As with all the other Person will be the custom class. br /Perhaps these operations combine the friends of 2 people
H3The name and type of the registers and segments usable by a 32 bit processor (Easily converted by a naming convention to 16 bit and even 64 bit)/H3PSince there are no other recipes on Assembly Language, it would be irrational to start anywhere but the basics of hardware in assembly programmi
H3The EFlags register, what they, what they do and when they are set./H3PThe flags are located in a single 32 bit register (EFlags), each a single bit, thus holding a value of 1 true, or 0 false. br / br /I will list the flags by name, location in the register and use: br /*location will
H3The general template for designing an Assembly code program/H3Pdiv class="code" br /TITLE br /COMMENT br / br / br /.MODEL SMALL br /.STACK 100H br /.DATA br / br /.CODE br /.486 br /INCLUDE io.mac br /main PR
H3How to implement the in assembly, the effect of the if statement, which is available to other languages./H3PBeing that Assembly is a low level language, as opposed to the high level most programmers are used to (eg: Java, C, C++, Perl, etc), it doesn't have all the same features that these l
