I hava a class declaration with a piece of code that I do not understand :
class Weapon { public: virtual void attack() const = 0; };
What does the const = 0
part means ?
class Shape { public: virtual void draw() const = 0; // = 0 means it is "pure virtual" ... }; This pure virtual function makes Shape an ABC. If you want, you can think of the "= 0;" syntax as if the code were at the NULL pointer.
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 C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword. It is used to tell the compiler to perform dynamic linkage or late binding on the function.
This is a pure virtual method (=0
) which is not supposed to change the data of the class (const
). You should provide an implementation in one of the classes deriving from Weapon
! See this: Difference between a virtual function and a pure virtual function
You are expected to derive from the Weapon
(can be considered interface) concrete classes, such as Axe
, Shotgun
, etc ... where you will provide the attack()
method.
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