RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  •  

    Operators which can not be overloaded in C++

    Any operator which operates on value(s) can be overloaded.

    For example,

    
    c = a + b;
    

    As the operator + operates on two values, it can be easily overloaded.

    Now look at the unary operator *. It operates on the variable name, not on the value.

    
    b = *a;
    

    where a is not a value, but a name.

    Similarly the following operators can not be overloaded.

    sizeof - The operator to find the size of a variable in memory

    ? -The ternary operator

    :: – The scope resolution operator

    . – The operator to access class members

    -> – The operator to access class members from a class pointer type variable

    * – The indirection operator

    4 Responses to “Operators which can not be overloaded in C++”

    1. Ofek says:

      Not sure I follow. How are the semantics of ‘*a’ any different than ‘b + a’ ? In both cases the data being processed is the *value* of a – in the dereferencing case, the value is just interpreted as an address. I don’t see how the *name* of a is an operand in either case.
      I think it is just a c++ design decision, for which operators to allow overloading. In different languages (LISP, Haskell, whatever) very different decisions were taken – I can’t see how it’s any semantic constraint (these operators operate on ‘name’ whereas others on ‘value’).

    2. Shibu says:

      Hi Ofek,
      thanks for your comment and interest in http://techievibes.com.
      From Stroustrup’s “The C++ Programming Language”, 3rd Edn, he says these operators take name rather than a value as their second argument and provide the primary means of referring to members.
      Hence, overloading these operators can lead to subtleties.

    3. loans says:

      I want to thank the blogger very much not only for this post but also for his all previous efforts. I found techievibes.com to be very interesting. I will be coming back to techievibes.com for more information.

    4. Great post, I bet a lot of work and research went into this article.

    Leave a Reply