This is a discussion on Overloadin within the C and C++ Programming forums, part of the Software Development category; Anyone here know about operator overloading? Please point me to some tutorial or code snippets for operator overloading....
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class. The following set of operators is commonly overloaded for user-defined classes: = (assignment operator) + - * (binary arithmetic operators) += -= *= (compound assignment operators) == != (comparison operators) C++ allows almost all operators to be overloaded, i.e.their meanings can be redefined for different objects. But Cannot introduce new operators only overload existing ones. So cannot create the exponentiation operator ** as in some languages. These C++ operators may not be overloaded :: scope resolution operator sizeof operator . object member selection .* pointer to member obj.*member : ? conditional expression At least one of the arguments must be a user-defined type. So for '+': Person + Person O.K. Person + int O.K. int + int Not O.K since that would redefine meaning of addition for primitives For example class MyClassthanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Karpagarajan : 04-24-2007 at 06:17 AM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |