what is an operator

Jul 5, 2010 at 6:31am
hi all,
we all know that c++ has many operators like new , = , << etc

but what essentially is an operator? how does it differ from a normal function?


Jul 5, 2010 at 6:49am
It is basically a normal function that can be called with a different syntax.
Jul 5, 2010 at 7:02am
so.. why is it termed operator then?? what makes it special?

In other words, by looking at the syntax (apart from the fact that the name starts with operator) will we be able to know whether a given function is an operator??

Can be override the address-of ( &) operator?
Jul 5, 2010 at 7:16am
An overloaded operator is a function. It's called operator because it looks like a regular operator at the call point (but it doesn't have to).
Now, regular operators are nothing like functions.
http://en.wikipedia.org/wiki/Operator_%28programming%29
Jul 5, 2010 at 7:38am
will we be able to write the custom operators??

say if I want to make "<?" as an operator.. will I be able to do so?

for example if i want to achieve the following functionality

1
2
3
std::string SomeString="hi"; 
  SomeString<?"$"
  std::cout << SomeString


desired output : $hi$
Jul 5, 2010 at 7:41am
No, you can only edit existing operators. Operators are basically functions that handle basic arithmetics with specific types. You can overload them; causing them to support more types, but you cannot create new ones.
Jul 6, 2010 at 1:41pm
The other answer is yes, you can override the address-of operator, however there is
rarely a need to do so, is annoying (see boost::addressof), and often times can end up
breaking code that unwittingly assumes that &obj gives a pointer to the first byte of
memory used for "obj" if "obj" is of class/struct type (and no multiple inheritance used).
Jul 11, 2010 at 10:18pm
@jsmith..
Overloading address of operator would help us a great deal in implementin the garbage collect..

Similarly can we also overload *?
Jul 11, 2010 at 11:14pm
Yes.

You can overload most of the operators in the C++ Language.
Jul 11, 2010 at 11:26pm
The only operators that can't be overloaded are . (dot), sizeof, ::, ?:, and, according to the FAQ, .* (whatever that is).
Topic archived. No new replies allowed.