In C++98, the null pointer was represented by the literal 0
(or in fact any constant expression whose value was zero). In C++11, we prefer nullptr
instead. But this doesn't work for pure virtual functions:
struct X { virtual void foo() = nullptr; };
Why does this not work? Would it not make total sense? Is this simply an oversight? Will it be fixed?
A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.
A virtual function is a member function of base class which can be redefined by derived class. A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract.
A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. An abstract class is a class in C++ which have at least one pure virtual function.
A pure virtual function doesn't have the function body and it must end with = 0 . For example, class Shape { public: // creating a pure virtual function virtual void calculateArea() = 0; }; Note: The = 0 syntax doesn't mean we are assigning 0 to the function.
Because the syntax says 0
, not expression or some other non-terminal matching nullptr
.
For all the time only 0
has worked. Even 0L
would be ill-formed because it does not match the syntax.
Edit
Clang allows = 0x0
, = 0b0
and = 00
(31.12.2013). That is incorrect and should be fixed in the compiler, of course.
The = 0
notation for virtual
functions wasn't literally "assign null" but rather a special notation which is actually deceptive: a pure virtual function can also be implemented.
With various context keywords it would make more sense to allow abstract
rather than = nullptr
and have abstract
be a context keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With