specifications of operator-overload
-
How could I find spefications of C++ operators overload, such as how many arguments should operator+() take, what should be the first operator of operator new(), operator int() qualifies only if it's a member function. I have <>, it lists a few of them.But it doesn't satisfy my needs.could someone help me?
-
How could I find spefications of C++ operators overload, such as how many arguments should operator+() take, what should be the first operator of operator new(), operator int() qualifies only if it's a member function. I have <>, it lists a few of them.But it doesn't satisfy my needs.could someone help me?
-
How could I find spefications of C++ operators overload, such as how many arguments should operator+() take, what should be the first operator of operator new(), operator int() qualifies only if it's a member function. I have <>, it lists a few of them.But it doesn't satisfy my needs.could someone help me?
Without googling for possibly more concise information, I do recall one thing about operator overloads: They must fit the signature of the standard operation; not with respect to type, which may be different, but with respect to number of arguments. Some operators have more strict requirements, e. g. assignmen operators should always return a value of type 'reference to the type that is being assigned to', but AFAIK these are not enorced by the compiler. It is still strongly recommended because otherwise youmight get unexpected behaviour when using these types with template libraries such as the STL. You could take a look at the interface of std::complex for a comprehensive list of overloaded operators and their signatures: complex operators P.S.: operator int() and the like, i. e. the casting operators, have their own special syntax: a) they may not have a return type (which would be redundant anyway), and b) they don't take an argument, because - as you noted - they only work as member functions. You should however consider to specify them as
explicit
, to prevent an accidental inappropriate use through automatic type conversion by the compiler.