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

F

FBergemann

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
 
B

benben

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top