Classes: Default / delete

code

Syntax

Default can be applied only on compiler-generated functions, while delete can be used on any function signature.

 

There are two special cases in the world of pointers.
One is void* pointers, because there is no way to dereference them, to increment or decrement them, etc.
The other is char* pointers, because they typically represent pointers to C-style strings, not pointers to individual characters.
Let’s assume That it should not be possible to call processPointer with void* or char* pointers – so we delete these functions.

 

Visibility of deleted functions: public vs private?

By convention, deleted functions are declared public, not private.
When client code tries to use a member function, C++ checks accessibility before deleted status.
When client code tries to use a deleted private function, some compilers complain only about the function being private, even though the function’s accessibility doesn’t really affect whether it can be used.

PROS

  • Any function can be deleted, not only member functions.
  • Express a clear image on what the programmer wanted to do

suppose we have a nonmember function that takes an integer and returns whether it’s a lucky number:

 

C++’s C heritage means that pretty much any type that can be viewed as vaguely numerical will implicitly convert to int, but some calls that would compile might not make sense:

 

 

Quiz – Will this compile?

Answer: No. Constructor with an int is not a compiler-generated function.

Answer: Yes, we can delete a particular overloaded function.

Answer: Yes, we can delete a function even if we don’t have other declarations of it.

Leave a comment

Your email address will not be published.