A function that ends in an =0
is called a deleted function
, this is useful when you don't want objects that use certain constructors (such as unique_ptr
which has a deleted copy ctor).
If a virtual
function is deleted then by standard the class becomes an abstract type. Because in most cases the prototype of a class and the class's function bodies are in separate files, this means that unless you explicitly outline in the prototype that you're overriding the deleted virtual function then you're NOT overriding the deleted virtual function. The compiler isn't supposed to just simply infer that you meant to put the function in there once it sees the implementation in a completely different file.
Remember that the prototype/implementation idea isn't the only way to write code, you can also put the implementation right in the class (which can be done if the code is small enough and you want to inline the function.) And to do that you need to again, explicitly override the deleted virtual function. So because you need to override it anyway, it makes perfect sense that you need to explicitly override it in the prototype. The function is still deleted otherwise.
For a concrete example: let's say you have a List.hpp, List.cpp and main.cpp
In List.hpp you have an abstract class and a regular class that inherits from the abstract class. In main you #include "List.hpp"
and not List.cpp, right? So the compiler has NO IDEA what's in that file (until it tries to compile it.) If you don't have the deleted virtual function overridden then the compiler thinks that you're simply trying to instantiate an abstract class and throws an error.
On the other hand, if you're compiling List.cpp, then the compiler will also throw an error, this time complaining that the function you're trying to write has not actually been defined. Because Base::deletedFunction()
is different from Derived::deletedFunction()
.