A pure virtual function is a virtual function whose declaration ends in =0:
class Base {
virtual void f() = 0;
Quote:
what values will be stored in virtual table if the class has pure virtual function
There is a subtle difference between pure- and non-pure-functions,
take a look:
Quote:
In the case of non-pure virtual functions, each entry in the vtable will refer to the final-overrider or a thunk that adapts the this pointer if needed.
In the case of a pure-virtual function, the entry in the vtable usually contains a pointer to a generic function that complains and aborts the program with some sensible message (pure virtual function called within this context or similar error message).
Quote:
what does 0 indicates in pure virtual functions
A 0 indicates that this is a pure virtual function! A pure virtual function implicitly makes the class it is defined for abstract (unlike in Java where you have a keyword to explicitly declare the class abstract). Abstract classes cannot be instantiated. Derived classes need to override/implement all inherited pure virtual functions. If they do not, they too will become abstract.
[EDITED]
Quote:
1) How does the compiler knows whether vtable has the pure virtual function entry or virtual function .
2) How we can make the call to base class pure virtual function. I tried to make a static call from derived class i got a linker error? .If possible can you please provide sample code
To 1): The compiler don't need to know! In case of pure-virtual-function is a NULL instead of address, it raises the exception. In case of a regular virtual function there is an address of the implemented function, it will be called.
To 2):
class A
{
public:
virtual void foo()=0;
};
class B : public virtual A
{
public:
void foo() { cout<<"foo()"; }
};
int main()
{
A* obj = new B();
obj->foo();
return 0;
}