Model
Model

Reputation: 61

Overriding operator

anybody me about constructor overriding..

i have this

void  operator delete(void*) {}

void  operator delete(void* p, void*) {}

in my class.. that looks like overloading(same function name and return type but different parameter list) but its overriding .. how its overriding..

anyone an explain me these two lines function.

Upvotes: 1

Views: 727

Answers (3)

icecrime
icecrime

Reputation: 76765

These are operator delete overloading and are called when delete is invoked on the object. From 3.7.3.2 (Deallocation functions) :

Each deallocation function shall return void and its first parameter shall be void*. A deallocation function can have more than one parameter. If a class T has a member deallocation function named operator delete with exactly one parameter, then that function is a usual (non-placement) deallocation function. If class T does not declare such an operator delete but does declare a member deallocation function named operator delete with exactly two parameters, the second of which has type std::size_t (18.1), then this function is a usual deallocation function.

And in 12.5 Free store :

When a delete-expression is executed, the selected deallocation function shall be called with the address of the block of storage to be reclaimed as its first argument and (if the two-parameter style is used) the size of the block as its second argument.

I'm not sure your second function can ever be called as it is not a 'usual deallocation function'.

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

void  operator delete(void*) {}
void  operator delete(void* p, void*) {}

These are custom deallocation functions. A deallocation function is called via a deleteexpression. E.g.

YourClass* p = new YourClass();    // Allocates memory & calls constructor
// ... whatever, then
delete p;                          // Calls destructor & deallocates memory

For your class the delete expression in the last line above would first call the destructor, and then it would call the single void* argument deallocation function that the class defines, the first of your two functions, if that deallocation is accessible.

However, it might be that the deallocation function is declared as private or protected, for the purpose of making it inaccessible. In the first case a delete expression outside the class' own code won't compile (inaccessible deallocation function). And if so then that may be the whole point -- or, don't be surprised if there's no point at all.

By the way, have a look at this tutorial. It's apparently the least bad free introduction to C++ on the net. Bruce Eckel's e-book "Thinking in C++" is also free, but it has some errors and misinformation (it used to be the other way around though, the tutorial at cplusplus.com used to be very bad, once).

Cheers & hth.,

Upvotes: 1

xscott
xscott

Reputation: 2420

They're overloading the delete operator, and the second one is using "placement delete". Placement new/delete are a hack so that you can have C++ constructors initialize the class on top of memory you provide (instead of ::new), and possibly with additional arguments like you see here. Google can fill you in with the gory details.

Upvotes: 0

Related Questions