Effective C++ - item 7 (memory mgt).

Discussion in 'C++' started by FBergemann@web.de, Aug 7, 2005.

  1. Guest

    Hi,

    i have a little question concerning Scott Meyer's Effective C++ (2nd
    edition), Item 7 "Be prepared for out-of-memory conditions". It's
    about his example for a class specific new_handler() management. Please
    note that i don't want to start a discussion about the
    purpose/usability of the approach. But i just want to understand some
    technical issue of the example.
    Here's the question:
    Why does he define the member function
    static void *operator new(size_t size);
    as a *static* function for the class?
    I can understand, that the
    static new_handler set_new_handler(new_handler p);
    Is defined as static. Because the user needs to invoke it for the class
    itself , before creating an instance of the class (
    X::set_new_handler(noMoreMemory) ).
    But why using static for operator new()? It is not invoked on the user
    level. Is it because the compiler needs to use it before there actually
    is an instance of the object? (so the same reason, but this time the
    compiler instead of the programmer?).
    Can anyone confirm of tell me the right answer for this?

    Thanks in advance!

    Frank Bergemann
     
    , Aug 7, 2005
    #1
    1. Advertising

  2. benben Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > i have a little question concerning Scott Meyer's Effective C++ (2nd
    > edition), Item 7 "Be prepared for out-of-memory conditions". It's
    > about his example for a class specific new_handler() management. Please
    > note that i don't want to start a discussion about the
    > purpose/usability of the approach. But i just want to understand some
    > technical issue of the example.
    > Here's the question:
    > Why does he define the member function
    > static void *operator new(size_t size);
    > as a *static* function for the class?
    > I can understand, that the
    > static new_handler set_new_handler(new_handler p);
    > Is defined as static. Because the user needs to invoke it for the class
    > itself , before creating an instance of the class (
    > X::set_new_handler(noMoreMemory) ).
    > But why using static for operator new()? It is not invoked on the user
    > level. Is it because the compiler needs to use it before there actually
    > is an instance of the object? (so the same reason, but this time the
    > compiler instead of the programmer?).
    > Can anyone confirm of tell me the right answer for this?
    >
    > Thanks in advance!
    >
    > Frank Bergemann
    >


    I guess even if you don't write the 'static' the compiler will still treat
    it as static so why not making it more explicit? The only difference between
    static and non-static member functions is that a non-static one take an
    addtional, hidden parameter the 'this pointer'. Using 'this pointer' simply
    doesn't make sense in operator new()

    But, I am just guessing, I might be wrong. In that case please correct me.

    Ben
     
    benben, Aug 7, 2005
    #2
    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. Don Kim
    Replies:
    9
    Views:
    562
    Gregg
    May 23, 2004
  2. Replies:
    1
    Views:
    6,085
    Karl Seguin [MVP]
    Jul 11, 2006
  3. KDawg44
    Replies:
    1
    Views:
    282
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Jun 12, 2007
  4. helveticus
    Replies:
    1
    Views:
    301
    Alexey Smirnov
    Apr 29, 2010
  5. Jim Ross [MVP]

    Problem with Exception Mgt. App Block on W2k Server

    Jim Ross [MVP], Dec 2, 2003, in forum: ASP .Net Security
    Replies:
    6
    Views:
    218
Loading...

Share This Page