operator new in custom namespace

Discussion in 'C++' started by dirk@dirkgregorius.de, Apr 11, 2007.

  1. Guest

    Why doesn't the standard allow to overload operator new/delete in my
    namespace, e.g.

    namespace Foo
    {
    void* operator new( std::size_t sz );
    void operator delete( void* address );
    }

    On the other hand I can overload new/delete per class:

    class Foo1
    {

    static void* operator new( std::size_t sz );
    static void operator delete( void* address );

    };

    And finally what happens if I have classes A and B that overwrite new
    and delete and a class C that now inherits from both. What is the
    expected behaviour there?



    Cheers,
    -Dirk
     
    , Apr 11, 2007
    #1
    1. Advertising

  2. wrote:
    > Why doesn't the standard allow to overload operator new/delete in my
    > namespace, e.g.
    >
    > namespace Foo
    > {
    > void* operator new( std::size_t sz );
    > void operator delete( void* address );
    > }
    >
    > On the other hand I can overload new/delete per class:
    >
    > class Foo1
    > {
    >
    > static void* operator new( std::size_t sz );
    > static void operator delete( void* address );


    'static' is implicit here, IIUIC. IOW, you don't need to use the
    keyword in the function declaration.

    >
    > };
    >
    > And finally what happens if I have classes A and B that overwrite new
    > and delete and a class C that now inherits from both. What is the
    > expected behaviour there?


    Not sure what having operator new in its own namespace would do. The
    language says that if a class has operator new overloaded, then the
    operator is used to allocate memory when constructing a dynamic object
    of that class (chosen over the global operator new). What are you
    trying to accomplish with the overloaded operator new in a namespace?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 11, 2007
    #2
    1. Advertising

  3. James Kanze Guest

    On Apr 11, 11:04 am, wrote:
    > Why doesn't the standard allow to overload operator new/delete in my
    > namespace, e.g.


    > namespace Foo
    > {
    > void* operator new( std::size_t sz );
    > void operator delete( void* address );
    > }


    Historical reasons, I would imagine.

    > On the other hand I can overload new/delete per class:


    > class Foo1
    > {
    > static void* operator new( std::size_t sz );
    > static void operator delete( void* address );
    > };


    > And finally what happens if I have classes A and B that overwrite new
    > and delete and a class C that now inherits from both. What is the
    > expected behaviour there?


    Ambiguous. The compiler should complain.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 11, 2007
    #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. Èý¹â
    Replies:
    1
    Views:
    600
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    8
    Views:
    355
    Neil Cerutti
    Dec 22, 2005
  3. mrstephengross
    Replies:
    3
    Views:
    421
    James Kanze
    May 10, 2007
  4. user
    Replies:
    1
    Views:
    447
    Kevin Spencer
    Jul 19, 2007
  5. xmllmx
    Replies:
    6
    Views:
    437
    xmllmx
    Feb 3, 2010
Loading...

Share This Page