![]() |
|
#1
|
|||
|
|||
Defining New C++ OperatorsI C++ we can overload an operators, that is fine!!
My question is how do I define a new operator apart from overloading the ones existing? Including how to set rules and principles of how the operator manipulates its operands? Thank you much!!!! |
|||
|
#2
|
||||
|
||||
Re: Defining New C++ OperatorsQuote:
Consider the following: CPP / C++ / C Code:
There are declarations for operators that would be inserted by the compiler if omitted and there are declarations for operators for which the compiler is unable to give any reasonable implementation. The operator declarations are as follows: assignment copy operator inequality operator equality operator less-than comparison operator greather-than comparison operator less-than-or-equal-to comparison operator greather-than-or-equal-to comparison operator basic arithmetic operators operators for assignment by addition and substraction and finally, streaming operators for I/O. Notably missing are ++ and -- operator declarations. At first glance, they appear to be relevant, but upon further investigation, their usage would be unclear. Hopefully this will give you an idea of how to use operators more effectively. One should carefully examine the implementation such that expected behavior of defined operators is intuitive and easily anticipated. As in the case of not defining ++ and -- operators, since we do not easily know from which side of the decimal point they would work, any choice for them appears to be wrong in at least one or more expectations of their behaviors. However, if we were declaring a class called Integer, we would then probably expect to see both ++ and -- declarations for postfix and prefix versions of these operators as the behavior of these would be easily anticipated by the user and therefore have an appropriate implementation. See: operator-overloading for more info. MxB |
|
#3
|
|||
|
|||
Re: Defining New C++ OperatorsQuote:
In standard C or C++ you can not create new operators different from the ones defined in the language. Period. Full stop. Regards, Dave |
|
#4
|
|||
|
|||
Re: Defining New C++ OperatorsWell, Bjarne Stroustup gives a good explaination why he didn't allowed "Home made operators" in C++. Here it is http://www.research.att.com/~bs/bs_faq2.html#overload-operator
Hope this reading will convince you |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| using bitwise operators for masks, ifdefs, inttype.h, etc... | Howard_L | C Programming Language | 3 | 20-Aug-2007 12:04 |
| How to print an equation in order of precedence of operators | shoegoe | C Programming Language | 7 | 02-Nov-2006 15:14 |
| Class definitions for operators | Darth Predator | C++ Forum | 1 | 12-Apr-2006 20:59 |
| overloading operators in a class. | fevershark | C++ Forum | 11 | 22-Feb-2006 20:34 |
| Overloading of operators | alcoholic | C++ Forum | 1 | 12-Dec-2005 05:37 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The