What are virtual functions?

Showing Answers 1 - 2 of 2 Answers

samiksc

  • Jan 25th, 2006
 

Virtual functions are declared by the base and derived classes as virtual. This makes the binding of the functions dynamic which will be done at runtime.

Suppose a base class pointer is pointing to a derived class object and a virtual function is called through this pointer. At runtime it will be detected that the pointer (although declared as base class pointer) is actually pointing to a derived class object, and hence derived class version of the virtual function will be called.

Useful when derived classes want to override a function of the base class. Slows down execution due to runtime binding.

  Was this answer useful?  Yes

samiksc

  • Jan 25th, 2006
 

Also increases memory consumption since a virtual function address table has to be maintained in RAM

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions