Advertisement
Drainedsoul

Placement New and Virtual Functions

Jan 22nd, 2012
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. /*
  2.     PROGRAM OUTPUT
  3.  
  4. Testing without vtable...
  5.  
  6. Size of object is 4
  7. 0
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5
  13. 6
  14. 7
  15. 8
  16. 9
  17.  
  18. Testing with vtable...
  19.  
  20. Size of object is 8
  21. 0
  22. 1
  23. 2
  24. 3
  25. 4
  26. 5
  27. 6
  28. 7
  29. 8
  30. 9
  31.  
  32. */
  33.  
  34.  
  35. #include <stdlib.h>
  36. #include <iostream>
  37.  
  38.  
  39. #define ARRAY_SIZE 10
  40.  
  41.  
  42. using namespace std;
  43.  
  44.  
  45. class VRoot {
  46.  
  47.  
  48.     public:
  49.    
  50.    
  51.         virtual void PrintValue () = 0;
  52.  
  53.        
  54. };
  55.  
  56.  
  57. class VRootImplemented : public VRoot {
  58.  
  59.  
  60.     private:
  61.    
  62.    
  63.         int value;
  64.        
  65.        
  66.     public:
  67.    
  68.    
  69.         VRootImplemented (int value) {
  70.        
  71.             this->value=value;
  72.        
  73.         }
  74.    
  75.    
  76.         void PrintValue () {
  77.        
  78.             cout << value << "\n";
  79.            
  80.         }
  81.  
  82. };
  83.  
  84.  
  85. class NoVRoot {
  86.  
  87.  
  88.     private:
  89.    
  90.    
  91.         int value;
  92.        
  93.        
  94.     public:
  95.    
  96.    
  97.         NoVRoot (int value) {
  98.        
  99.             this->value=value;
  100.        
  101.         }
  102.        
  103.        
  104.         void PrintValue () {
  105.        
  106.             cout << value << "\n";
  107.        
  108.         }
  109.  
  110.  
  111. };
  112.  
  113.  
  114. void main () {
  115.  
  116.     //  TEST 1
  117.     cout << "Testing without vtable...\n\nSize of object is " << sizeof(NoVRoot) << "\n";
  118.    
  119.     //  Allocate array
  120.     NoVRoot * p_no_v=(NoVRoot *)malloc(sizeof(NoVRoot)*ARRAY_SIZE);
  121.     //  Theoretically check malloc success here
  122.    
  123.     //  Fill array
  124.     for (int i=0;i<ARRAY_SIZE;++i) new (p_no_v+i) NoVRoot (i);
  125.    
  126.     //  Call PrintValue() on each item
  127.     for (int i=0;i<ARRAY_SIZE;++i) p_no_v[i].PrintValue();
  128.    
  129.     //  Theoretically clean up here
  130.    
  131.     //  TEST 2
  132.     cout << "\nTesting with vtable...\n\nSize of object is " << sizeof(VRootImplemented) << "\n";
  133.    
  134.     //  Allocate array
  135.     VRootImplemented * p_v=(VRootImplemented *)malloc(sizeof(VRootImplemented)*ARRAY_SIZE);
  136.    
  137.     //  Fill array
  138.     for (int i=0;i<ARRAY_SIZE;++i) new (p_v+i) VRootImplemented (i);
  139.    
  140.     //  Call PrintValue() on each item
  141.     for (int i=0;i<ARRAY_SIZE;++i) {
  142.    
  143.         VRoot * test=&(p_v[i]);
  144.        
  145.         test->PrintValue();
  146.    
  147.     }
  148.    
  149.     //  Theoretically clean up here
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement