'Can a non-virtual function be equal to 0?

Can a non-virtual function be equal to 0, e.g., something like

void foo() = 0

where the keyword virtual is not in front?



Solution 1:[1]

No.

When applied to a virtual function, = 0 makes it pure virtual. It means nothing on a non-virtual function.

Update: A virtual function does not necessarily begin with the virtual keyword, in cases where the function overrides a virtual function in its base class.

Solution 2:[2]

It is not a function being equal to zero, it is a designator of a pure virtual function.

Knowing that, it's clear that non-virtual functions cannot be designated pure virtual.

However, if s base class has a virtual function that your class is overriding, you don't need to repeat the virtual designator, yet you are allowed to mark the function pure virtual.

Solution 3:[3]

= 0 does not mean that the function is null. It is just a syntax for telling compiler that this is pure virtual function. You can't have pure non virtual function, because it will be impossible to instantiate such class and it will be impossible to override the function in child classes. But the code you've shown may appear in a program is foo() is declared as virtual in parent class.

Solution 4:[4]

virtual marks a function virtual and = 0 marks it pure virtual in addition. A non-virtual, pure function... what would that be?

So no, that doesn't work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sergey Kalinichenko
Solution 3 Alexey Guseynov
Solution 4 cadaniluk