Search code examples
c++delete-operatorscope-resolution

What is the purpose of "::delete" in C++?


I'm currently looking at C++ code that uses ::delete to delete a pointer.

A meaningless example of this is:

void DoWork(ExampleClass* ptr)
{
    ::delete ptr;
}

What is the purpose of using the delete keyword in this way?


Solution

  • In some cases, the operator delete might be redefined -actually overloaded- (for example, your Class might define it and also define operator new). By coding ::delete you say that you are using the standard, "predefined", deletion operator.

    A typical use case for redefining both operator new and operator delete in some Class: you want to keep a hidden global set of all pointers created by your Class::operator new and deleted by your Class::operator delete. But the implementation of your delete will remove that pointer from the global set before calling the global ::delete