Click here to Skip to main content
15,914,066 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I know that virtual table of a class stores the addresses of virtual functions .My question is, what values will be stored in virtual table if the class has pure virtual function and what does 0 indicates in pure virtual functions

I searched online but I couldn't able to find good answers

Thanks in advance,
Posted

A pure virtual function is a virtual function whose declaration ends in =0:
C++
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):

C++
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;
}
 
Share this answer
 
v7
Comments
krish0969 24-Sep-15 10:02am    
Thanks for the explanation .I still have some doubts in mind

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
Leo Chapiro 24-Sep-15 10:20am    
I have edited, take a look
krish0969 24-Sep-15 10:27am    
For the second answer it will call the derived class function.I am trying to understand how we can call the base class function without a static call(calling from derived class function using scope resolution operator) so that it will give runtime error
Leo Chapiro 24-Sep-15 10:34am    
Not sure whether I understood right, do you mean:

A* obj = new A(); ?

This will raise a compiler's error: "cannot allocate an object of abstract type 'A'"
krish0969 24-Sep-15 10:55am    
In the answer you mentioned "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)"

Can you explain at what situation will get this error in programtically
 
Share this answer
 
Comments
krish0969 24-Sep-15 10:05am    
Hi Pallini,

Thanks for the response .I have gone through the link provided .If possible can you please also answer the questions which I asked in comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900