operator new override/overload?

Discussion in 'C++' started by Shark, Jan 13, 2006.

  1. Shark

    Shark Guest

    Hi, if we need to change the behavior of operator new, it is called
    overriding or overloading? My other question is, if we change the
    behavior of operator new, do we use malloc to do that or we use
    operator new?
     
    Shark, Jan 13, 2006
    #1
    1. Advertising

  2. Shark

    Fred Zwarts Guest

    "Shark" <> wrote in message news:...
    > Hi, if we need to change the behavior of operator new, it is called
    > overriding or overloading? My other question is, if we change the
    > behavior of operator new, do we use malloc to do that or we use
    > operator new?


    It is not clear what you want. C++ has several operators new. Which one do you need to change?
    C++ has the possibility to define a new operator for a class. This is the safest way to go.
    It is also possible to override the global new operators, but this needs a lot of care,
    since these global new operators are also used in the C++ library functions.
    (This means that on many platforms the standard operator new sits already in the shareable libraries,
    so you should use a static link to get a consistent use of the changed new operators.)
    In addition you should change the delete operators as well to match your new operators.
    You could use malloc and free.

    F.Z.
     
    Fred Zwarts, Jan 13, 2006
    #2
    1. Advertising

  3. * Shark:
    > Hi, if we need to change the behavior of operator new, it is called
    > overriding or overloading?


    operator new is a static function, and on that account it would be
    'overload'.

    However, operator delete is also a static function, but behaves much like a
    virtual function with dynamic (run-time) dispatch:


    #include <iostream>
    #include <ostream>
    #include <memory>

    struct Base { virtual ~Base() {} };

    struct DerivedA: Base
    {
    static void operator delete( void* p )
    {
    std::cout << "Deallocating A." << std::endl;
    ::eek:perator delete( p );
    }
    };

    struct DerivedB: Base
    {
    static void operator delete( void* p )
    {
    std::cout << "Deallocating B." << std::endl;
    ::eek:perator delete( p );
    }
    };

    int main()
    {
    std::auto_ptr<Base> pA( new DerivedA );
    std::auto_ptr<Base> pB( new DerivedB );
    }

    which yields the output

    Deallocating B.
    Deallocating A.

    as if operator delete were a virtual function.

    So in the case of operator delete it's not entirely inappropriate to talk
    about 'override'.

    What about operator new then, viewed as a kind of same sort of function as
    operator delete?

    Well, it isn't. Operator new is more like a constructor (you don't override
    constructors, and you don't override operator new), and operator delete is
    more like a virtual destrutor (you do override virtual destructors).


    > My other question is, if we change the
    > behavior of operator new, do we use malloc to do that or we use
    > operator new?


    You can use anything you want, it depends on what you want. But given that
    you ask, i.e. not sure, my advice would be to _not_ overload operator new.
    Except as a learning experiment -- don't use features you don't understand
    in production code, especially not dangerous, low-level features.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 13, 2006
    #3
    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. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    413
    Jakob Bieling
    Aug 8, 2003
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    397
    Nick Hounsome
    Jan 25, 2004
  3. dolphin
    Replies:
    2
    Views:
    3,222
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Nov 2, 2007
  4. Stephan Kämper
    Replies:
    2
    Views:
    210
    Stephan Kämper
    Jan 24, 2005
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    282
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page