Advertisement
Guest User

Untitled

a guest
Feb 8th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3.  
  4. class PhysObject
  5. {
  6. public:
  7. virtual void evaluatePhysics() = 0;
  8. };
  9.  
  10. class Particle : public PhysObject
  11. {
  12. public:
  13. int position;
  14.  
  15. void evaluatePhysics()
  16. {
  17. this->position = this->position + 1;
  18. }
  19. };
  20.  
  21. int main(int argc, const char **argv)
  22. {
  23.         // valid creation of a Particle object on the stack
  24.         Particle newParticle;
  25.  
  26.         // Test calling ->evaluatePhysics() through a pointer - works
  27.         Particle *newParticlePtr = &newParticle;
  28.         newParticlePtr->evaluatePhysics();
  29.         printf("newParticlePtr position: %d\n", newParticlePtr->position);
  30.  
  31.         // Malloc some memory the size of a Particle
  32.         Particle *particle = (Particle *)malloc(sizeof(Particle));
  33.  
  34.         // Copy the valid newParticle into the malloced memory
  35.         // I think this fails because "particle" points to uninitialized memory
  36.         // and the next line needs to invoke the "assignment operator" on
  37.         // the object pointed to by "particle".
  38.         *particle = newParticle;
  39.  
  40.         // fails :(
  41.         particle->evaluatePhysics();
  42.  
  43.         return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement