Why these operators cant be overloaded?

Discussion in 'C++' started by ambar.shome@gmail.com, Aug 11, 2005.

  1. Guest

    Hi,

    As you know there are few operators in C++ which cant be overloaded.

    They are:

    .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr ,
    dynamic_cast , const_cast , reinterpret_cast .

    Theremust be some reason for this restriction for each of the
    operators.

    Does anyone know the exact reason for such restriction on each of them?
     
    , Aug 11, 2005
    #1
    1. Advertising

  2. erm Guest

    you can overload operators new and delete
     
    erm, Aug 11, 2005
    #2
    1. Advertising

  3. Guest

    I dont think "new" can be overloaded. "operator new" can be overloaded
    but not "new" operator. Same for delete. Follow any traditional book
    for confirmation.
     
    , Aug 11, 2005
    #3
  4. Ian Guest

    wrote:
    > I dont think "new" can be overloaded. "operator new" can be overloaded
    > but not "new" operator. Same for delete. Follow any traditional book
    > for confirmation.
    >

    And the difference is?

    You can have your own class local operator new and your own global one.

    Ian
     
    Ian, Aug 11, 2005
    #4
  5. Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++ :

    >> I dont think "new" can be overloaded. "operator new" can be overloaded
    >> but not "new" operator. Same for delete. Follow any traditional book
    >> for confirmation.
    >>

    > And the difference is?


    The new operator first calls operator new() and then the constructor(s).

    --
    ___________ 11/08/2005 10:37:58
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Il faut donc que les hommes commencent
    -'(__) par n'être pas fanatiques pour mériter
    _/___(_) la tolérance. -- Voltaire, 1763
     
    Serge Paccalin, Aug 11, 2005
    #5
  6. "Serge Paccalin" <> wrote in message
    news:11oreoy6oimgd$.dlg@canttouchthis-127.0.0.1...
    > Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++ :
    >
    > >> I dont think "new" can be overloaded. "operator new" can be overloaded
    > >> but not "new" operator. Same for delete. Follow any traditional book
    > >> for confirmation.
    > >>

    > > And the difference is?

    >
    > The new operator first calls operator new() and then the constructor(s).


    Very artificial distinction.

    Or, similarly it could be argued that "->" operator cannot be overloaded,
    but "operator->" can (because the "->" operator first calls "operator->"
    and then the "->" operator).

    - Risto -
     
    Risto Lankinen, Aug 11, 2005
    #6
  7. Serge Paccalin wrote:
    > Le jeudi 11 août 2005 à 10:33, Ian a écrit dans comp.lang.c++ :
    >
    > >> I dont think "new" can be overloaded. "operator new" can be overloaded
    > >> but not "new" operator. Same for delete. Follow any traditional book
    > >> for confirmation.
    > >>

    > > And the difference is?

    >
    > The new operator first calls operator new() and then the constructor(s).


    There are new statement and operator new (the same applies to delete).
    new statement is a language construct that calls operator new and then
    an object constructor. new statement can not be redefined or overloaded
    (macro aside), but operator new can be overloaded.

    The same name for language construct and operator has lead to much
    confusion.
     
    Maxim Yegorushkin, Aug 11, 2005
    #7
  8. Guest

    Hi,

    I think you all are not following my original question. Once again
    please follow my query given below:
    ---------------------------------------------------------------------------------------------------------------------------------
    As you know there are few operators in C++ which cant be overloaded.


    They are:


    .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr ,
    dynamic_cast , const_cast , reinterpret_cast .


    Theremust be some reason for this restriction for each of the
    operators.


    Does anyone know the exact reason for such restriction on each of them?



    ----------------------------------------------------------------------------------------------------------------------------

    I am desperately looking for answer . Will anybody help me on this?
     
    , Aug 11, 2005
    #8
  9. <> wrote in message
    news:...
    > As you know there are few operators in C++ which cant be overloaded.
    >
    > ., .*, ::, ?: , new , delete , sizeof , typeid , static_casr ,
    > dynamic_cast , const_cast , reinterpret_cast .
    >
    > Theremust be some reason for this restriction for each of the
    > operators.
    >
    > Does anyone know the exact reason for such restriction on each of them?


    http://www.research.att.com/~bs/bs_faq2.html#overload-dot

    - Risto -
     
    Risto Lankinen, Aug 11, 2005
    #9
  10. Pete Becker Guest

    erm wrote:

    > you can overload operators new and delete
    >


    Well, yes and no. You can overload operator new and operator delete, but
    you cannot overload the new and delete operators, which was what the
    original question was about.

    operator new and operator delete are functions: void *operator
    new(size_t) and void operator delete(void*) in their simplest forms.
    When you say 'new int' you're using the new operator to create an int on
    the heap; it calls operator new to get the memory, then constructs the
    value as appropriate for the type. Similarly, when you say 'delete ptr'
    you're using the delete operator, which destroys the value and then
    calls operator delete to release the memory.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Aug 11, 2005
    #10
  11. Pete Becker Guest

    Risto Lankinen wrote:

    >>
    >>The new operator first calls operator new() and then the constructor(s).

    >
    >
    > Very artificial distinction.
    >


    It may be artificial, but it's exactly what the standard says. The
    problem is the confusing names 'operator new', which is a function and
    can be overloaded, and the 'new' operator, which is a keyword and does
    what's described above.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Aug 11, 2005
    #11
  12. Guest

    Hi,

    Thank you very much for the link. But there are few operators left on
    which I dint get any info. Those are

    typeid , static_casr ,
    dynamic_cast , const_cast , reinterpret_cast

    Do u or anyone have any idea about them?

    Again thanking you for the link.
     
    , Aug 11, 2005
    #12
  13. Le jeudi 11 août 2005 à 10:49, Risto Lankinen a écrit dans
    comp.lang.c++ :

    >>> And the difference is?

    >>
    >> The new operator first calls operator new() and then the constructor(s).

    >
    > Very artificial distinction.


    ISO-IEC 14882:1998, page 78:

    8 A /new-expression/ obtains storage for the object by calling an
    /allocation function/ (3.7.3.1). If the /new-expression/ terminates
    by throwing an exception, it may release storage by calling a
    deallocation function (3.7.3.2). If the allocated type is a
    non-array type, the allocation function's name is 'operator new'
    and the deallocation function's name is 'operator delete'. If the
    allocated type is an array type, the allocation function's name is
    'operator new[]' and the deallocation function's name is 'operator
    delete[]'. [/Note:/ an implementation shall provide default
    definitions for the global allocation functions (3.7.3, 18.4.1.1,
    18.4.1.2). A C++ program can provide alternative definitions of
    these functions (17.4.3.4) and/or class-specific versions (12.5). ]

    > Or, similarly it could be argued that "->" operator cannot be overloaded,
    > but "operator->" can (because the "->" operator first calls "operator->"
    > and then the "->" operator).


    This is just silly.

    --
    ___________ 11/08/2005 15:16:50
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Il faut donc que les hommes commencent
    -'(__) par n'être pas fanatiques pour mériter
    _/___(_) la tolérance. -- Voltaire, 1763
     
    Serge Paccalin, Aug 11, 2005
    #13
  14. msalters Guest

    schreef:

    > Hi,
    >
    > Thank you very much for the link. But there are few operators left on
    > which I dint get any info. Those are
    >
    > typeid , static_casr ,
    > dynamic_cast , const_cast , reinterpret_cast


    Casts can't be overloaded because you can define cast operators on
    your own types. E.g.

    class X{};
    class Y {
    operator X();
    };

    typeid/dynamic_cast are related to RTTI, which is implemented using'
    compiler magic. For that reason, it can't be overloaded. That's similar
    to new (calling constructors is compiler magic too).

    HTH,
    Michiel Salters
     
    msalters, Aug 12, 2005
    #14
  15. Operator . (dot) could in principle be overloaded using the same technique
    as used for ->.
    However, doing so can lead to questions about whether an operation is meant
    for the
    object overloading. or an object referred to by .

    class Y {
    public:
    void f();
    // ...
    };

    class X { // assume that you can overload .
    Y* p;
    Y& operator.() { return *p; }
    void f();
    // ...
    };

    void g(X& x)
    {
    x.f(); // X::f or Y::f or error?
    }


    In ClassName::Member neither ClassName nor Member are expressions with
    values;
    ClassName and Member are names known to the compiler and :: performs a
    (compile time)
    scope resolution rather than an expression evaluation.

    One could imagine allowing overloading of x::y where x is an object rather
    than a namespace or a class,
    but that would contrary to first appearences - involve introducing new
    syntax (to allow expr::expr).


    ?: is not allowed to overload because,
    A function overloading expr1? expr2:expr3 would not be able to guarantee
    that
    only one of exper2 and expr3 was executed as its in the hand of programmer.

    sizeof is not allowed to overload because,
    A function overloading sizeof can return anything they want..!!!!

    Sizeof cannot be overloaded because built-in operations, such as
    incrementing a pointer into an array implicitly depends on it.

    X a[10];
    X* p = &a[3];
    X* q = &a[3];
    p++; // p points to a[4] thus the integer value of p must be sizeof(X)
    larger than the integer value of q

    Thus, sizeof(X) could not be given a new and different meaning by the
    programmer without violating basic language rules.



    <> wrote in message
    news:...
    > Hi,
    >
    > As you know there are few operators in C++ which cant be overloaded.
    >
    > They are:
    >
    > ., .*, ::, ?: , new , delete , sizeof , typeid , static_casr ,
    > dynamic_cast , const_cast , reinterpret_cast .
    >
    > Theremust be some reason for this restriction for each of the
    > operators.
    >
    > Does anyone know the exact reason for such restriction on each of them?
    >
     
    Girish Shetty, Aug 12, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -Steve-
    Replies:
    2
    Views:
    377
    - Steve -
    Jul 28, 2003
  2. Brad Eck
    Replies:
    20
    Views:
    46,230
    Alex Vinokur
    Oct 30, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,090
    Smokey Grindel
    Dec 2, 2006
  4. Bangalore
    Replies:
    4
    Views:
    1,277
    Fabio Fracassi
    Jul 27, 2005
  5. Nagaraj
    Replies:
    1
    Views:
    893
    Lionel B
    Mar 1, 2007
Loading...

Share This Page