[CPP] Function pointers and virtual functions

Function Pointers

  • Functions are stored in memory, in code segament.
  • You can ask for the address of them.
  • You can store these in function pointers.
enter image description here
void (*g1)() = NULL;
The above code means g1 is a function pointer, the function takes no parameter and it returns void.
Note that (*g1)(); and g1() both call f1()
enter image description here
Here we change again, we assign g1 to g2:
Remember g2 is just a pointer, g1 assign to g2 so g2 just assign the address of g1 and g1 points to f1, so when we call g2, what we actually did is to call f1();

Callback Function

A callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time
Briefly speaking, callback function is a function pointer(a address), you know this pointer or address, you can use it whenever and wherever you want. Therefore, you can use it as function argument.
enter image description here

v-tables

non-virtual

  • For normal/default (non-virtual) functions:
  • Type of pointer determines function to call
  • Type is known at compile-time

virtual

  • Finds out the actual function to call based upon the object type at runtime-much difficult- slower
  • look-up “which function should i really call”
  • works in the same way as Java functions

Possible Implementation

How could virtual functions be implemented?
One possible implementation uses vtables(virtual function tables) and vpointers(pointers to a vtable)
It is quite easy, when you have a virtual function, you add it to the vtable. If there is a virtual sub-function in the sub-class, you substitute it. But we still save the previous position(maybe function in super-class). It is because we need to save the index(position).
enter image description here
Note: Only virtual functions appear in vtables, no need to record non-virtual functions

Virtualness is inherited in C++

  • Virtual-ness is inherited
  • If a function is virtual in the base class then the function is virtual in the derived class(es)
  • Including destructor! (If virtual in base class then destructors of derived classes are virtual)
  • Even when the keyword virtual is not used in the derived class
  • All functions in Java are virtual by default
  • unless the function is final, if it is final you cannot re-implemented in sub-classes.

look up virtual function

Looking up which function to call is slower than calling a non-virtual function:
1. Go to object itself
2. Go to the vtable(following the vpointer)
3. Look up which function to call from index
4. Call the function

评论

  1. Note: If a add a member function to an existing class, the object usually will not be bigger because functions act on objects but there is one exception. Adding the first virtual function may add a vtable pointer!

    回复删除

发表评论

此博客中的热门博文

[MLE] W2 Multivariate linear regression

[MLE] W1 Introduction

[AIM] MetaHeuristics