Why is a pure virtual function initialized by 0?
The reason =0
is used is that Bjarne Stroustrup didn't think he could get another keyword, such as "pure" past the C++ community at the time the feature was being implemented. This is described in his book, The Design & Evolution of C++, section 13.2.3:
The curious =0 syntax was chosen ...
because at the time I saw no chance of
getting a new keyword accepted.
He also states explicitly that this need not set the vtable entry to NULL, and that doing so is not the best way of implementing pure virtual functions.
what happens if I initialize a virtual function other than 0? will it create a pure virtual function or what else happens in memory?
There is no such thing as initializing a virtual function to a value. The grammar = 0
is a pure-specifier, not an initializer. It is special grammar that is given special meaning; it designates that the virtual function is a pure-virtual function and must be implemented by any classes inheriting from it.
= 1
following a virtual
function declaration has no grammatical meaning in C++ and therefore is ill-formed. If you didn't get a compile error, then your compiler is either buggy or you weren't "initializing" a virtual
function at all.
What does `= 0` mean in the decalartion of a pure virtual function?
The = 0
makes the function pure virtual, rendering the class an abstract class.
An abstract class basically is a kind of interface, which derived classes need to implement in order to be instantiable. However, there's much more to this, and it is some of the very basics of object-oriented programming in C++. If you don't know these, you need to go back to the textbook and read up. There's no way you can advance without understanding them.
That said, see this related question for some explanations of what virtual and pure virtual functions are. And as always, the C++ FAQ is an excellent resource for such questions.
Why in C++ 'virtual' and '=0' is both needed to describe a method is abstract?
Your thoughts were right.
So why =0 symbol is needed? Does it means that a child class must
define this function, and that's to say when there is no =0, some
child classes are not forced to define this method?
Basically you can:
- Make a method non-virtual
This doesn't allow any class deriving from the class that implements the method (through either public
or protected
) to change the method's behavior.
- Make a method
virtual
This allows (but doesn't enforce) any class deriving from the class that implements the method (through either public
or protected
) to change the behavior of the method in the base class. You don't even have to call the original base class method anymore so you can make severe changes if needed.
- Make a method
pure virtual
(virtual = 0
)
This enforces that any class deriving from the class that implements the method (through either public
or protected
) to implement some kind of behavior/body for this method. If the deriving class does not provide an implementation then this class will instantly become abstract itself. This allows to omit the behavior/body of the method in the base class and because of this it is not allowed to directly instantiate a class that has one or more pure virtual methods (abstract class).
why MSVS allows NULL as pure virtual function specifier?
According to MSDN, NULL
is defined as something that is close enough to 0
for MSVC++ to swallow. That's it.
Try doing #undef NULL
before that code, and it should properly break compilation.
Related Topics
Atomic Double Floating Point or Sse/Avx Vector Load/Store on X86_64
What Is Special About Numbers Starting With Zero
Sin and Cos Give Unexpected Results For Well-Known Angles
Parsing Command Line Arguments in C++
How to Install the Raspberry Pi Cross Compiler on My Linux Host Machine
How to Sum Up Elements of a C++ Vector
(-2147483648≫ 0) Returns True in C++
When Should I Use C++ Private Inheritance
How Does Delete[] Know It's an Array
Programmatically Create Static Arrays At Compile Time in C++
How to Check If a Cpu Supports the Sse3 Instruction Set
C++ Semantics of 'Static Const' VS 'Const'
Is 0 a Decimal Literal or an Octal Literal
Why Do People Use _ (Double Underscore) So Much in C++
Can Inner Classes Access Private Variables