Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `= 0` mean in the decalartion of a pure virtual function? [duplicate]

Possible Duplicates:
C++ Virtual/Pure Virtual Explained
What's the difference between virtual function instantiations in c++
Why pure virtual function is initialized by 0?

This is a method in some class declaration that someone gave me. And I don't know what '..=0' means. What is it?

virtual void Print() const = 0;
like image 437
or.nomore Avatar asked Dec 12 '22 19:12

or.nomore


2 Answers

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.

like image 197
sbi Avatar answered Jan 22 '23 20:01

sbi


It means that the virtual function is pure, meaning that you cannot call it as such: the function doesn't have any code to it, hence the = 0. Only by deriving the class and overriding the function you can call it. The class with pure virtual functions cannot be instantiated so they are called abstract classes, interfaces in some languages.

like image 31
mike3996 Avatar answered Jan 22 '23 21:01

mike3996