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

7

8

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?

Costello answered 20/8, 2018 at 15:17 Comment(0)
R
10

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

Rising answered 20/8, 2018 at 15:23 Comment(2)
But the allocation function ClassName::operator new (size_t) should call another allocation function ::operator new(size_t) and not use a new-expression. Use of a new-expression will cause the constructor to run multiple times. Using a delete-expression inside a deallocation function will similarly cause trouble.Deannadeanne
It should be noted that the definition of a function can seldom be replaced (not overloaded because the signature is exactly the same). That is what "redefine" means technically, and this is guaranteed to work only with a few functions in the standard library, including ::operator delete.Pearlene
E
9

This is using the delete expression, but with the optional :: prefix.

Syntax

::(optional) delete expression (1)

...

Destroys object(s) previously allocated by the new expression and releases obtained memory area.

Using the :: prefix will affect lookup:

The deallocation function's name is looked up in the scope of the dynamic type of the object pointed to by expression, which means class-specific deallocation functions, if present, are found before the global ones.

If :: is present in the delete expression, only the global namespace is examined by this lookup.

Epileptoid answered 20/8, 2018 at 15:20 Comment(0)
A
7

delete in C++ is an operator in the same way = is. It therefore can be re-implemented. And just like = , the re-implementation is specific to the class to which it is referring. Adding the :: makes sure that the delete operator that we are calling is the global one and not one specific to a given class.

This can be useful when you re-implement the delete for a specific class and then want to refer to the real one.

Alvord answered 20/8, 2018 at 15:24 Comment(0)
T
3

Using ::delete has quite a few uses in a large scale program, in your example it doesn't make a lot of sense but using it in a larger context does enable the following:

  • Global variable access, so if you have local and global x. Using ::x refers to the global and not the local
  • Define a function outside of a class, not sure why you would want or need to do that but the functionality is there
  • Access static class variables
  • Distinguish between the same variable name between two or more classes in the case of multiple inheritance

The link below has a pretty good explanation and examples to mull over.

Source: https://www.geeksforgeeks.org/scope-resolution-operator-in-c/

Territerrible answered 20/8, 2018 at 15:25 Comment(0)
V
2

::delete is synonymous with delete :: is for scope. E.g Classname:: means anything within that class' scope. In this instance :: means anything that is scoped by default (e.g anything you don't need to include a namespace for)

delete frees a pointer from the heap. Not doing this will mean when the program exits, that chunk of memory is still counted as being used by the OS (the OS will usually clean this up but it's bad practice to not free your pointers)

So usually

int* intpointer = new int(5); //do something with intpointer delete intpointer

Vaas answered 20/8, 2018 at 15:19 Comment(1)
Yes I do! very tiredVaas
C
1

Class-specific overloads
Deallocation functions (17-24) may be defined as static member functions of a class. These deallocation functions, if provided, are called by delete-expressions when deleting objects (17,19,21) and arrays (18,20,22) of this class, unless the delete expression used the form ::delete which bypasses class-scope lookup. The keyword static is optional for these function declarations: whether the keyword is used or not, the deallocation function is always a static member function.

This implies that ::delete is not equal to delete. The difference here is delete can be overridden and is specific to your object/class. ::delete is the global

I know some cases you should NOT use "::delete", it will not work

Basically, when deallocating, the destructor looked by the compiler is the most local to the global - if a destructor is not found in the current scope, it looks one level above until it reaches the global (that is always there). Using ::, changes the starting scope used by the compiler to be the global one.

See more here. There is a whole section for it.

Cassilda answered 20/8, 2018 at 15:29 Comment(0)
P
0

It is a possibility that your class has overloaded new and delete. So ::delete indicates that we are referring to global scope and not the ones that have been over-ridden in the current class.

Useful link: What are "::operator new" and "::operator delete"?

Philosophical answered 20/8, 2018 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.