Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. Bad Example:
  2.  
  3. class Geometry {
  4. virtual void thingy() = 0;
  5. }
  6.  
  7. class Circle : public Geometry() {
  8. void thingy() override;
  9. }
  10.  
  11. class Square : public Geometry() {
  12. void thingy() override;
  13. }
  14.  
  15. void do_something(Geometry *object) {
  16. object->thingy();
  17. }
  18.  
  19. This will create a VTable, a vtable will always do a second jmp instruction when you call the function, as it does not know what function to call, it needs to inspect the virtual table call and then call the correct function.
  20.  
  21. This on the other hand will always call the function correctly without an extra call, and the size of the structures will be smaler, as there will not be a VTable
  22.  
  23. template<typename T>
  24. thingy(T Geometry) {
  25.  
  26. }
  27.  
  28. template<Circle>
  29. thingy(Circle& circle) {
  30.  
  31. }
  32.  
  33. Template<Square>
  34. thingy(Square& circle) {
  35.  
  36. }
  37.  
  38. template<typename T> do_something(T object) {
  39. thingy<T>(object);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement