Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- class PhysObject
- {
- public:
- virtual void evaluatePhysics() = 0;
- };
- class Particle : public PhysObject
- {
- public:
- int position;
- void evaluatePhysics()
- {
- this->position = this->position + 1;
- }
- };
- int main(int argc, const char **argv)
- {
- // valid creation of a Particle object on the stack
- Particle newParticle;
- // Test calling ->evaluatePhysics() through a pointer - works
- Particle *newParticlePtr = &newParticle;
- newParticlePtr->evaluatePhysics();
- printf("newParticlePtr position: %d\n", newParticlePtr->position);
- // Malloc some memory the size of a Particle
- Particle *particle = (Particle *)malloc(sizeof(Particle));
- // Copy the valid newParticle into the malloced memory
- // I think this fails because "particle" points to uninitialized memory
- // and the next line needs to invoke the "assignment operator" on
- // the object pointed to by "particle".
- *particle = newParticle;
- // fails :(
- particle->evaluatePhysics();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement