What does 0 mean in pure virtual function

The program below does not compile for obvious reasons:

#include <iostream>
using namespace std;

class A {
 public:
  A() { pVirt(); }
  virtual void pVirt() const = 0 { count<<"A::pVirt()"; }
};

int main() {
 A aObj;
 aObj.pVirt();
 reutrn 0;
}

      

Questions: 1. The 0 in the signature "virtual void pVirt () const = 0" means that ?, is it specifying a NULL memory offset in the vtable, or is it just a syntax constraint?

  • If 0 is the offset of NULL memory (if so), then why does VC ++ not allow you to specify a different memory address, and that is why we cannot call a pure virtual function from an external constructor (MAYBE because the vtable is created after the object fully engineered.)?
+3


source to share


4 answers


Its simple syntax means the function is pure virtual. It doesn't really matter. C ++ designers could just as well use pure

or abstract

instead of = 0

. I suspect that the only reason for this was not that they didn't want to introduce a new reserved word into the language (as this breaks any existing code that already uses only the reserved word as an identifier).



(There is no need to make such a reserved word, as it is a context sensitive keyword, but the concept of context sensitive keywords has only recently entered mainstream use, and the syntax is much older.)

+4


source


0 in signature "virtual void pVirt () const = 0" means what ?,

The part =0

is called a pure-specifier. This makes the virtual function clean and abstract.

A pure virtual function does not need to be defined. You can optionally provide a definition outside of the class, and not an abstract abstract class still needs to override the function.

class A
{
 public:
     virtual ~A() {};
     virtual void f() =0;
};

void A::f() { std::cout << "A::f" << std::endl; } //optional

      

The definition f

does not make the class non-abstract, so you cannot create an instance A

:

A a; //error - A is abstract

      

Also, the output class must override A::f

to be non-abstract:



class B : public A {};

B b; //error : B is still an abstract class as it didn't override A::f

      

AND

class C : public A { void f() {} };

C c; //okay : C override A::f

      

And the derived class implementation can choose to call the base class implementation:

class D : public A { void f() {  A::f(); } }; //defaults to A::f

D d; //okay : D override A::f, but calls A::f internally

      

Hope it helps.

+4


source


= 0

in a function declaration is just syntax: two sequence of tokens means the function is pure, that's all. And also you cannot replace any of the tokens with anything else: = 0L

not legal, for example.

And the reason you are getting the error is simply because the syntax is not allowed. For historical reasons, if nothing else. If you want to provide a definition for a pure virtual function, you must do so outside the class.

And if you provide a definition for a pure virtual function, you can call it from the constructor; you just need to deactivate the virtual call mechanism; for example A::pVirt()

. If the actual function call involves dynamic resolution, and the resolution results in a pure virtual function, it is undefined whether the function is defined or not. The motivation here is to let you not define it; as usual, a virtual function needs to be defined, whether you call it or not, because the compiler has to put its address in vtable

, and if it is not defined, there is no address. Executing a pure virtual function tells the compiler that it shouldn't address in vtable

. So you don't need to define it. But you can get surprises if you try to call the function through the vtable.

+2


source


= 0

after a virtual function means "this is a pure virtual function, it must be implemented in a derived function". As in your example, it is still possible to implement this functionality. It has no other meaning as such - and you cannot use other numbers, addresses, or whatever. You can have multiple functions with =0

and it will still be the same.

+1


source







All Articles