Draft:Deleted functions (C++)
Submission declined on 9 November 2024 by KylieTastic (talk).
Where to get help
How to improve a draft
You can also browse Wikipedia:Featured articles and Wikipedia:Good articles to find examples of Wikipedia's best writing on topics similar to your proposed article. Improving your odds of a speedy review To improve your odds of a faster review, tag your draft with relevant WikiProject tags using the button below. This will let reviewers know a new draft has been submitted in their area of interest. For instance, if you wrote about a female astronomer, you would want to add the Biography, Astronomy, and Women scientists tags. Editor resources
|
Deleted functions[1] are a feature of the C++ programming language that allow the programmer to prevent specific functions from being called. The most common usage of deleted functions is when the programmer wants to disable copying of a class object.
Deleted functions were added in C++11.
Usage
[edit]Below is a very simple usage of deleted functions:
void printNum(int x) {
std::cout << x << '\n';
}
printNum(char) = delete;
printNum(bool) = delete;
The following code forbids the user from having a char or a boolean type as an argument for the printNum
function, which are otherwise acceptable due to Integral promotion[2]. If we were to call the function and use a char or a boolean as an argument, we would get a compile error.
Deleted functions are, however, used mostly to disable copying of a class object.
Example(Example const&) = delete;
Example& operator=(Example const&) = delete;
The example above, disables the copy constructor and the copy assignment operator using the delete
keyword.
See also
[edit]References
[edit]- ^ "Function declaration - cppreference.com". en.cppreference.com. Retrieved 2024-11-09.
- ^ "Implicit conversions - cppreference.com". en.cppreference.com. Retrieved 2024-11-09.
- in-depth (not just passing mentions about the subject)
- reliable
- secondary
- independent of the subject
Make sure you add references that meet these criteria before resubmitting. Learn about mistakes to avoid when addressing this issue. If no additional references exist, the subject is not suitable for Wikipedia.