Let see what they are, what you can change and the safe rules to handle them.
First of all let see what happens when you write something like:
C * pC = new C;
- Enough memory is allocated to contain the object requested
- The constructor of C is called to initialize the object the lays in the memory allocated
The first point in the sequence above is the only think you can change, the way the memory is allocated, the new operator uses for this task what is called: operator new.
So the pseudo code for C * pC = new C; could be:
- Call operator new
- Construct an object of the type request at the location returned from previous step
void * operator new(size_t);
so if you want change the way the new operator allocates the memory for your type then you need to rewrite the operator new.
As you already know when you specify a name in a scope ( for example a method name in a class ) this will hide the same name in the scopes that are containing your actual ( the base class scope for example ). So rewriting your operator new what you do is to hide the other forms of operator new. For instance these forms are:
- void * operator new(std::size_t, std::nothrow_t) throw();
- void * operator new(std::size_t, void *);
- void * operator new(std::size_t, T);
No comments:
Post a Comment